stop()和detroy()方法已被jdk启用,不推荐使用
终止方法:通常的做法是提供一个boolean型的终止变量,当这个变量置为false,则终止线程的运行。
【示例11-3】终止线程的典型方法(重要)
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 | public class TestThreadCiycle implements Runnable { String name; boolean live = true;// 标记变量,表示线程是否可中止; public TestThreadCiycle(String name) { super(); this.name = name; } public void run() { int i = 0; //当live的值是true时,继续线程体;false则结束循环,继而终止线程体; while (live) { System.out.println(name + (i++)); } } public void terminate() { live = false; } public static void main(String[] args) { TestThreadCiycle ttc = new TestThreadCiycle("线程A:"); Thread t1 = new Thread(ttc);// 新生状态 t1.start();// 就绪状态 for (int i = 0; i < 100; i++) { System.out.println("主线程" + i); } ttc.terminate(); System.out.println("ttc stop!"); }} |