使用简单对象作为锁对象比Java中任何其他更复杂的对象都好?
我使用对象作为锁,例如成员函数使用简单对象作为锁对象比Java中任何其他更复杂的对象都好?,java,locking,synchronized,Java,Locking,Synchronized,我使用对象作为锁,例如成员函数 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 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
}
}
}
这两者有什么区别