大神们,为什么这个同步锁没用呢,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();
}
}
}
}
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();
}
}
}
}