class Req1 implements Runnable{ static Req1 req1 = new Req1(); static int i=0; @Override public void run() { for(int j = 0;j<10000;j++){ i++; } } public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(req1); Thread thread2 = new Thread(req1); thread1.start(); thread2.start(); thread1.join(); thread2.join(); System.out.println(i); } }
循环次数太少了,CPU瞬间执行完,两个线程没有交叉执行,或者交叉次数太少且没有出现i++被中断执行的情况