在主线程中启动一个线程t,当主线程向t发出停止执行,t用一个boolean值来控制run中的不断执行过程。t进行终止操作,这里的终止不是是使得线程执行完run方法。
public class CountupThread extends Thread { private long counter=0; //计数器 private volatile boolean shutdownRequested=false; public void shutdownRequest(){ shutdownRequested=true; interrupt(); } public boolean isShutdownRequested(){ return shutdownRequested; } public final void run(){ try{ while(!shutdownRequested){ doWork(); } }catch(InterruptedException e){ //真在sleep的时候interrupted }finally{ System.out.println("doWorkShutdown:counter="+counter); } } private void doWork() throws InterruptedException { counter++; System.out.println("do Worker counter:"+counter); Thread.sleep(500); } }
public class Main { public static void main(String[] args){ System.out.println("main:Begin"); try{ CountupThread t=new CountupThread(); t.start(); Thread.sleep(10000); System.out.println("main:shutDownRequest"); t.shutdownRequest(); System.out.println("main:join"); t.join();//等待线程结束,可以用isAlive来检测是否结束 }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("main:End"); } }
运行结果:
main:Begin
do Worker counter:1
do Worker counter:2
do Worker counter:3
do Worker counter:4
do Worker counter:5
do Worker counter:6
do Worker counter:7
do Worker counter:8
do Worker counter:9
do Worker counter:10
do Worker counter:11
do Worker counter:12
do Worker counter:13
do Worker counter:14
do Worker counter:15
do Worker counter:16
do Worker counter:17
do Worker counter:18
do Worker counter:19
do Worker counter:20
main:shutDownRequest
main:join
doWorkShutdown:counter=20
main:End
这里,考虑到了:
安全性:当t收到结束信号以后并没有马上结束,而是先改变结束标识shutdownRequest。这时对象不会被突然地终止而受到破坏。
生命性:改变结束标识后调用了interrupt方法,这主要是为了使得当线程正在sleep、wait的时候也能够顺利中断掉。为了使抛出异常的时候也能终止处理故使用了try...finally块。shutdownRequest的作用是如果任务很繁重,它会执行到下次循环的。
响应性:终止请求发出后要尽快进入终止处理。
关于interrupt的一点说明
当一个线程调用interrupt方法后,线程会进入两种状态:一、线程中断状态改变;二、线程没有中断,而是进入InterruptedException(通常是线程处于sleep、wait、join)。
public class Thread1 extends Thread { @Override public void run(){ try { System.out.println("1"); System.out.println("1"); sleep(5000); //休息5秒吧 System.out.println("2"); System.out.println("2"); } catch (InterruptedException e) { //如果sleep时进入异常,那么上面的2就没有输出 e.printStackTrace(); //这时线程没有中断,会继续输出下面的3 } System.out.println("3"); System.out.println("3"); }
public class Test { public static void main(String[] args){ Thread t1=new Thread1(); t1.start(); t1.interrupt(); } }
运行结果:
1
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
1
3
3
at interrupt.Thread1.run(Thread1.java:9)
如果要让线程由InterruptedException进入中断,应:
try { ... ... } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
对于第一种情况:Thread.interrupt()方法不会中断一个正在运行的线程。它只是发去一个信号。
public class Thread2 extends Thread{ @Override public void run(){ int i=0; while(true) System.out.println(i++); } }
public class Test { public static void main(String[] args) throws InterruptedException{ Thread t2=new Thread2(); t2.start(); Thread.sleep(100); t2.interrupt(); } }
运行结果中可以知道,线程在一直运行,根本没有停止!
应该Thread.interrupted()检查是否发生中断。Thread.interrupted()能告诉你线程是否发生中断,并将清除中断状态标记,所以程序不会两次通知你线程发生了中断。
public class Thread2 extends Thread{ @Override public void run(){ int i=0; //while(true) // System.out.println(i++); while(!Thread.interrupted()) System.out.println(i++); } }