publicclassTest{privateinta=0;publicstaticvoidmain(String[]args){Testtest=newTest();for(inti=0;i<=10;i++){newThread(newRunnable(){@Overridepublicvoidrun(){test.setA(test.getA()+1);}}).start();}System.out.println(test.getA());}publicintgetA(){returna;}publicsynchronizedvoidsetA(inta){this.a=a;}}如果我把synchronide放到run方法里则执行正确?为什么呢?锁不是当前对象吗?只有一个test对象啊?
Helenr
相关分类