public class MyThread extends Thread { public static int n = 0;
public void run() { int m = n; yield(); m++; n = m; } public static void main(String[] args) throws Exception { MyThread myThread = new MyThread (); Thread threads[] = new Thread[100]; for (int i = 0; i < threads.length; i++) threads[i] = new Thread(myThread); for (int i = 0; i < threads.length; i++) threads[i].start(); for (int i = 0; i < threads.length; i++) threads[i].join(); System.out.println("n = " + MyThread.n); } }在执行上面代码的可能结果如下:n = 59看到这个结果,可能很多读者会感到奇怪。这个程序明明是启动了100个线程,然后每个线程将静态变量n加1。最后使用join方法使这100个线程都运行完后,再输出这个n值。按正常来讲,结果应该是n = 100。可偏偏结果小于100。