Thread类之线程中断

来源:互联网 发布:淘宝直通车添加宝贝 编辑:程序博客网 时间:2024/06/07 00:56

Java的Thread类中有三个与线程中断有关的方法,这三个方法相对来说还是比较简单的,稍稍介绍一下这三个方法之间的差别:

  • public void Thread.interrupt()    --中断线程
  • public boolean Thread.isInterrupted()    --判断当前线程是否被中断
  • public boolean Thread.interrupted()    --判断当前线程是否被中断,并清除当前中断状态


接下来用三个小例子来说明一下这三个方法的运用场景:

/** * Thread类中的interrutp方法并不一定百分百中断线程 * jdk的api中有这么一句话:如果当前线程没有中断它自己(这在任何情况下都是允许的) * 并且api中说明:如果线程调用 Object类中的 wait、join等一系列阻塞方法时, * interrutp()才会致使线程中断,并抛出一个InterruptedException。  * 注:该程序必须要在jvm的server模式下运行,在client模式下运行看不到效果 * 有关jvm的server模式与client模式有兴趣的朋友可以到网上查找相关资料,都解释的比较详细 * 如果使用的是jdk7即以上版本,则默认就是使用server模式,不需要进行额外设置 */public class FirstInterruptDemo extends Thread {public void run(){while(true){System.out.println("线程运行中....");//让主程序能够执行中断Thread.yield();}}public static void main(String[] args){Thread t = new FirstInterruptDemo();t.start();//去中断线程,看线程是否会被中断t.interrupt();System.out.println("==========中断已执行==========");}}

其运行结果如下:可以看到,即使执行了线程中断,但是线程依旧处于运行状态,并没有停止下来

小提示:尽可能快速的关闭虚拟机,否则执行时间过长,由于控制台能够显示的行数有限,可能看不到“中断已执行这句话的输出”




/** * Thread类中的interrutp方法并不一定百分百中断线程 * jdk的api中有这么一句话:如果当前线程没有中断它自己(这在任何情况下都是允许的) * 并且api中说明:如果线程调用嗯啦 Object类中的 wait、join等一系列阻塞方法时, * interrutp()才会致使线程中断,并抛出一个InterruptedException。  * 在线程未阻塞的情况下,interrutp()更像一个通知,或者说标记,告诉该线程要中断 * 需要用isInterrupted()方法判断线程是否有这样一个中断标记 * */public class SecondInterruptDemo extends Thread{public void run(){while(true){if(Thread.currentThread().isInterrupted()){System.out.println("========线程退出========");break;}System.out.println("线程运行中....");//让主线程能够执行中断Thread.yield();}}public static void main(String[] args)throws Exception{Thread t = new SecondInterruptDemo();t.start();//让线程有执行Thread.sleep(50);//去中断线程,看线程是否会被中断t.interrupt();System.out.println("==========中断已执行==========");}}


其运行结果如下:



/** * Thread类中的interrutp方法并不一定百分百中断线程 * jdk的api中有这么一句话:如果当前线程没有中断它自己(这在任何情况下都是允许的) * 并且api中说明:如果线程调用嗯啦 Object类中的 wait、join等一系列阻塞方法时,  *  * 因为线程处于阻塞状态,调用interrutp会致使线程中断,并抛出一个InterruptedException。 * 可在catch中执行退出线程的操作 */public class ThirdInterruptDemo extends Thread{public void run(){synchronized (this){while(true){try {System.out.println("线程执行中");this.wait();} catch (InterruptedException e){System.out.println("线程退出");break;}}}}public static void main(String[] args)throws Exception{Thread t = new ThirdInterruptDemo();t.start();//让线程有执行Thread.sleep(50);//去中断线程,看线程是否会被中断t.interrupt();System.out.println("==========中断已执行==========");}}

执行结果如下:



前面有提到过isInterrupted()方法与interrupted()方法之间的区别:

public boolean Thread.isInterrupted()    --判断当前线程是否被中断
public boolean Thread.interrupted()    --判断当前线程是否被中断,并清除当前中断状态


/** *Thread.interrupted()测试当前线程是否已经中断。线程的中断状态 由该方法清除。 *换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。  */public class FouthInterruptDemo extends Thread{public void run(){for(int i = 0; i < 5; i++){//让主线程能够执行中断Thread.yield();System.out.println(Thread.interrupted());                        //System.out.println(Thread.currentThread().isInterrupted());               }}public static void main(String[] args){Thread t = new FouthInterruptDemo();t.start();//执行中断t.interrupt();}}

其运行结果如下:

感兴趣的朋友可以将Thread.interrupted()换成Thread.isInterrupt()看看结果有什么不同


原创粉丝点击