大家看看,为什么我加了同步,有时候程序运行也会出现不同步的情况呢
class RunImp implements Runnable {
public static int count = 0;
public void run() {
while (count < 5) {
synchronized (this) {
count++;
System.out.println(Thread.currentThread().getName() + "--------"
+ count);
}
}
}
}
public class ThreadTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Thread(new RunImp()).start();
new Thread(new RunImp()).start();
}
}
尝试很多次,有一次运行结果是这样的:
Thread-0--------2
Thread-0--------3
Thread-0--------4
Thread-0--------5
Thread-1--------2
大多情况都同步的
|