java多线程状态及interrupt()方法

在java多线程中,线程的状态有 NEW, Runnable, Blocked, Waiting, Timed_Waiting, Terminated. 这是java虚拟机下的线程状态,与操作系统下的线程状态略有不同。线程状态以枚举类型定义在Thread.State中,并且当前线程可以通过getState()方法获取当前线程的状态。Runnable其实可以有两种状态,一种是获得了cpu,这在运行,这里把它表示为Running,另一种在队列中,等待调度。

这些状态之间的转化关系可以通过下面的转化图表示:

有些过程没有写,不然太乱了。

这里重点说一下stop()以及interrupt。

对于stop()方法,直接终止线程,释放线程所获的资源,但是在释放过程中会造成对象状态不一致,从而使程序进入未知的境地,已经很久不推荐使用了。可以通过设定标志位来检测线程终止状态,使线程自动终止。简略实现如下:

boolean stop = false;

public void run(){

        while(!stop){

        }

}

在需要终止线程的地方把stop设置为true即可。


interrupt方法好多初学者会感到困惑,发现一些情况下并不能终止线程。在java API中有对此详细的说明:

如果线程在调用 Object 类的 wait()wait(long) 或 wait(long, int) 方法,或者该类的join()join(long)join(long, int)sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException

如果该线程在可中断的通道上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个 ClosedByInterruptException

因此,可以看出,interrupt之后作用到wait() join() 已经sleep()上。 2014阿里巴巴笔试题的附加题中对这个知识进行了终点考核,其代码如下:

[java]  view plain  copy
  1. public class TestInterrupt {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.           
  9.         Thread thread1 = new Thread(){  
  10.             public void run(){  
  11.                 try{  
  12.                     long time = System.currentTimeMillis();  
  13.                     while(System.currentTimeMillis()-time<2000){  
  14.                     }                     
  15.                     System.out.println("A1");  
  16.                 }  
  17.                 catch(Exception e)  
  18.                 {  
  19.                     System.out.println("B1");  
  20.                 }                 
  21.             }  
  22.         };  
  23.         thread1.start();  
  24.         thread1.interrupt();  
  25.           
  26.         //在线程sleep状态下进行中断  
  27.         Thread thread2 = new Thread(){  
  28.             public void run(){  
  29.                 try {  
  30.                     this.sleep(2000);  
  31.                     System.out.println("A2");  
  32.                 } catch (Exception e) {  
  33.                     // TODO Auto-generated catch block  
  34.                     System.out.println("B2");  
  35.                 }  
  36.             }  
  37.               
  38.         };  
  39.           
  40.         thread2.start();  
  41.         thread2.interrupt();  
  42.           
  43.         //在线程wait状态下进行中断,其中wait()没有在同步块中  
  44.         Thread thread3 = new Thread(){  
  45.             public void run(){  
  46.                 try {  
  47.                     this.wait(2000);  
  48.                     System.out.println("A3");  
  49.                 } catch (Exception e) {  
  50.                     // TODO Auto-generated catch block  
  51.                     System.out.println("B3");  
  52.                 }  
  53.             }  
  54.               
  55.         };  
  56.           
  57.         thread3.start();  
  58.         thread3.interrupt();  
  59.           
  60.         //在线程wait状态下进行中断,其中wait()在同步块中  
  61.         Thread thread4 = new Thread(){  
  62.             public void run(){  
  63.                 try {  
  64.                     synchronized(this){  
  65.                         this.wait(2000);  
  66.                         System.out.println("A4");}  
  67.                 } catch (Exception e) {  
  68.                     // TODO Auto-generated catch block  
  69.                     System.out.println("B4");  
  70.                 }  
  71.             }  
  72.               
  73.         };  
  74.           
  75.         thread4.start();  
  76.         thread4.interrupt();  
  77.       
  78.           
  79.         try{  
  80.             thread4.start();  
  81.             System.out.println("A5");  
  82.         }  
  83.         catch(Exception e)  
  84.         {  
  85.             System.out.println("B5");  
  86.             System.out.println(e.toString());  
  87.         }  
  88.           
  89.           
  90.     }  
  91.   
  92. }  

我们可以运行,发下运行结果如下(不考虑执行顺序):

A1 

B2

B3

B4

B5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值