java同步 静态变量的变化
public class abc implements Runnable{
//共享资源变量
static int count = 0;
@Override
public synchronized void run() {
for (int i = 0; i < 2; i++) {
count=count+1;
System.out.println(Thread.currentThread().getName()+":"+count);
}
}
public static void main(String[] args) throws InterruptedException {
abc syncTest1 = new abc();
abc syncTest2 = new abc();
Thread thread1 = new Thread(syncTest1,"thread1");
Thread thread2 = new Thread(syncTest2, "thread2");
thread1.start();
thread2.start();
}
}
某次结果:
thread1:1
thread2:2
thread2:4
thread1:3
无法理解最后两行