java吧 关注:1,243,915贴子:12,719,786
  • 0回复贴,共1

求老鸟,多线程新手问题

取消只看楼主收藏回复

大神们,为什么这个同步锁没用呢,for循环里越耗时,c和a的值越小,根本有没锁住
public class TestCallable {
public static int a = 0;
public static AtomicInteger c = new AtomicInteger(0);
public static void main(String[] args) {
iThread t1 = new iThread();
iThread t2 = new iThread();
t1.start();
t2.start();
try
{
System.out.println("atomic c: "+c);
System.out.println("int a : "+a);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class iThread extends Thread{
@Override
public void run() {
synchronized(this)
{
for(int i =0; i<1000; i++){
TestCallable.a++;
TestCallable.c.incrementAndGet();
}
}
}
}


IP属地:广东1楼2017-05-13 22:32回复