在java中同步 - 正确使用 [英] synchronized in java - Proper use

查看:122
本文介绍了在java中同步 - 正确使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个在多进程(Threads)中使用的简单程序。

I'm building a simple program to use in multi processes (Threads).

我的问题还有更多要理解 - 当我必须同步使用保留字时?

My question is more to understand - when I have to use a reserved word synchronized?

我是否需要在任何影响骨骼变量的方法中使用这个词?

Do I need to use this word in any method that affects the bone variables?

我知道我可以把它放在任何非静态的方法上,但我想了解更多。

I know I can put it on any method that is not static, but I want to understand more.

谢谢!

这是代码:

public class Container {
// *** data members ***
public static final int INIT_SIZE=10;  // the first (init) size of the set.
public static final int RESCALE=10;   // the re-scale factor of this set.
private int _sp=0;
public Object[] _data;
/************ Constructors ************/
public Container(){
    _sp=0;
    _data = new Object[INIT_SIZE];
}
public Container(Container other) {  // copy constructor
    this();
    for(int i=0;i<other.size();i++) this.add(other.at(i));
}

/** return true is this collection is empty, else return false. */
public synchronized boolean isEmpty() {return _sp==0;}

/** add an Object to this set */
public synchronized void add (Object p){
    if (_sp==_data.length) rescale(RESCALE);
    _data[_sp] = p;  // shellow copy semantic.
    _sp++;
}   

/** returns the actual amount of Objects contained in this collection */
public synchronized int size() {return _sp;}

/** returns true if this container contains an element which is equals to ob */
public synchronized boolean isMember(Object ob) {
    return get(ob)!=-1;
}

/** return the index of the first object which equals ob, if none returns -1 */
public synchronized int get(Object ob) {
    int ans=-1;
    for(int i=0;i<size();i=i+1)
        if(at(i).equals(ob)) return i;
    return ans;
}

/** returns the element located at the ind place in this container (null if out of range) */
public synchronized Object at(int p){
    if (p>=0 && p<size()) return _data[p];
    else return null;
}


推荐答案

使类安全 - 线程访问是一个复杂的主题。如果您没有这样做以了解线程,您应该尝试找到一个可以帮助您的库。

Making a class safe for multi-threaded access is a complex subject. If you are not doing it in order to learn about threading, you should try to find a library that does it for you.

话虽如此,一个开始的地方是想象两个单独的线程以交替的方式逐行执行一个方法,看看会出现什么问题。例如,上面写的add()方法容易受到数据破坏。想象一下,thread1和thread2同时或多或少地调用add()。如果thread1运行第2行,在它到达第3行之前,thread2运行第2行,则thread2将覆盖thread1的值。因此,您需要一些方法来防止线程像这样交错。另一方面,isEmpty()方法不需要同步,因为只有一条指令将值与0进行比较。再一次,很难让这些东西正确。

Having said that, a place to start is by imagining two separate threads executing a method line by line, in an alternating fashion, and see what would go wrong. For example, the add() method as written above is vulnerable to data destruction. Imagine thread1 and thread2 calling add() more or less at the same time. If thread1 runs line 2 and before it gets to line 3, thread2 runs line 2, then thread2 will overwrite thread1's value. Thus you need some way to prevent the threads from interleaving like that. On the other hand, the isEmpty() method does not need synchronization since there is just one instruction that compares a value to 0. Again, it is hard to get this stuff right.

这篇关于在java中同步 - 正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆