class MyThread implements Runnable{ private boolean flag = true ; // 定义标志位 public void run(){ int i = 0 ; while(this.flag){ System.out.println(Thread.currentThread().getName() +"运行,i = " + (i++)) ; } } public void stop(){ this.flag = false ; // 修改标志位 } }; public class StopDemo{ public static void main (String args []) { the MyThread My = new new the MyThread (); the Thread T = new new the Thread (My, "threads"); // build thread object t.start (); // start threads the try { the Thread .sleep ( 30 ); } the catch (Exception E) { } my.stop (); // modify flag stopped } };