1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
public class TestStop implements Runnable{
private boolean flag = true;
@Override public void run() { while (flag){ System.out.println("run...Thread"); } }
public void stop(){ this.flag = false; }
public static void main(String[] args) { TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) { System.out.println("main" + i); if(i == 900){ testStop.stop(); System.out.println("线程停止了"); break; } } } }
|