同步块不会锁定对象。

3

有人能解释一下,为什么这里的synchronized块没有锁定变量_c

public class static_thread  implements Runnable{
private static Integer _c=0;
public static void main(String[] args) throws Throwable {
  for(int i=0;i<100000;i++){
    if(i%2==0){_c++;}
  }
  System.out.println("one thread: "+_c);
  Thread[] t=new Thread[50];
  _c=0;
  for(int i=0;i<t.length;i++){
    t[i]=new Thread(new static_thread(i, i*(100000/50)));
    t[i].start();
  }
  for(Thread _:t){_.join();}
  System.out.println("+one thread: "+_c);//wrong answer!
}
  public void run() {
    for(int i=s;i<(s+l);i++){
      if(i%2==0){
        synchronized (_c) {//y u no lock?!
          _c++;//Inconsistence, not thread-safe, so what is synchronized block is doing here?
        }
      }
    }
  }
  int s,l;
  public static_thread(int s, int l) {
    this.s = s;
    this.l = l;
  }
}

每次运行,我都会得到新的错误值。

1个回答

9
通过这样做
_c++

您正在更改_c所持有的引用。这相当于

int value = _c.intValue();
_c = Integer.valueOf(value + 1);

因此。
synchronized (_c) {

每次同步使用不同的对象。

请记住,synchronized块会锁定一个对象,而不是一个变量。

您应该始终使用不能更改值的变量与synchronized,即使将其设置为final

如果您需要某个原因的计数器,请考虑使用AtomicInteger


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接