public class TestMain { public static void main(String[] args) { Runnable r1 = new MyThread(); // 启动两个线程控制同一个对象r1 Thread t1 = new Thread(r1); Thread t2 = new Thread(r1); t1.start(); t2.start(); } } class MyThread implements Runnable{ int i; public void run() { while(true){ System.out.println(i++); try { long tmp = (long)((Math.random())*1000); Thread.sleep(tmp); } catch (InterruptedException e) { e.printStackTrace(); } if( i == 3 ){ // Debug的时候会有一个线程的i值不会等于3(也就不会break) // 正常运行的时候,两个线程都会break // 【问题:】为什么Debug的时和正常运行的情况不一样呢? System.out.println(" i = 3"); break; } } } }//源代码片段来自云代码http://yuncode.net