为什么使锁对象私有封装锁以使客户端代码无法获取它?
对象的内在锁
public class A{
private final Set<Integer> set = new HashSet<Integer>();
public synchronized void addInt(int i){
set.add(i);
}
}
私人锁
public class B{
private final Set<Integer> set = new HashSet<Integer>();
public void addInt(int i){
synchronized(set){
set.add(i);
}
}
}