public class Counter {
private volatile int count = 0;
//若要线程安全执行执行count++,需要加锁
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
为什么 这种自增 需要 加锁, 我理解为 没有 使用他对象 都有自己的
count , 完全不会影响他 其他 对象啊
除非用 static 的,请 大牛 们 帮忙解释下。。。。谢谢