<分区>
Possible Duplicate:
Avoid synchronized(this) in Java?
这两段代码有什么区别?各有什么优缺点?
1)
public class Example {
private int value = 0;
public int getNextValue() {
synchronized (this) {
return value++;
}
}
}
2)
public class Example {
private final Object lock = new Object();
private int value = 0;
public int getNextValue() {
synchronized (lock) {
return value++;
}
}
}