有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用简单对象作为锁对象进行锁定比Java中任何其他更复杂的对象都好?

我用一个对象作为锁,比如一个成员函数

public class Test {
    private Object a_obj = new Object();
    private ArrayList a_List = new ArrayList();

    public void test() {
        synchronized (a) {
            // do whatever you want with the a_List here
        }
    }
}

但如果我这么做:

public class Test {
    private ArrayList a_List = new ArrayList();

    public void test() {
        synchronized (a_List) {
            // do whatever you want with the a_List here
        }
    }
}

这两者有什么区别


共 (0) 个答案