interrupt()只是改变中断状态而已. interrupt()不会中断一个正在运行的线程。这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号, 这样受阻线程就得以退出阻塞的状态。更确切 的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞, 那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。 如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到InterruptedException异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。 线程A在执行sleep,wait,join时,线程B调用线程A的interrupt方法,的确这一个时候A会有InterruptedException 异常抛出来. 但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。 如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态 ,所以线程A不会抛出 InterruptedException,而会一直执行着自己的操作. 当线程A终于执行到wait(),sleep(),join()时,才马上会抛出 InterruptedException. 若没有调用sleep(),wait(),join()这些方法,即没有在线程里自己检查中断状态自己抛出InterruptedException的 话, 那InterruptedException是不会被抛出来的. 具体使用见实例1,实例2。 注意1:当线程A执行到wait(),sleep(),join()时,抛出InterruptedException后,中断状态已经被系统复位了, 线程A调用Thread.interrupted()返回的是false public static boolean interrupted ()Since: API Level 1Returns a Returns
public boolean isInterrupted ()Since: API Level 1Returns a Returns
线程A正在使用sleep()暂停着: Thread.sleep(100000); 如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用 a.interrupt(); 令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例 当在sleep中时 线程被调用interrupt()时,就马上会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程. 2. wait() & interrupt() 线程A调用了wait()进入了等待状态,也可以用interrupt()取消. 不过这时候要小心锁定的问题.线程在进入等待区,会把锁定解除,当对等待中的线程调用interrupt()时 ,会先重新获取锁定,再抛出异常.在获取锁定之前,是无法抛出异常的. 3. join() & interrupt() 当线程以join()等待其他线程结束时,当它被调用interrupt(),它与sleep()时一样, 会马上跳到catch块里. 注意,是对谁调用interrupt()方法,一定是调用被阻塞线程的interrupt方法.如在线程a中调用来线程t.join(). 则a会等t执行完后在执行t.join后的代码,当在线程b中调用来 a.interrupt()方法,则会抛出InterruptedException,当然join()也就被取消了。 实例1: static void interruptDemo() { Thread t1=new Thread(new InterruptDemoRunnable(),"first"); Thread t2=new Thread(new InterruptDemoRunnable(t1),"second"); t1.start(); t2.start(); } class InterruptDemoRunnable implements Runnable { long id=0; static long count=0; Thread t; InterruptDemoRunnable() { this(null); } InterruptDemoRunnable(Thread t) { id=count ; this.t=t; } boolean blRun=true; public void run() { int cnt=0; while(blRun) { if(t!=null) { if(cnt==2) { t.interrupt(); if(t.isInterrupted()) System.out.println("set the thread " t.getName() " to be Interrupted"); } } else { try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("First Interrupted when sleep"); System.out.println( "Interrupted is " Thread.interrupted());//@1 } try { Thread.sleep(1); } catch(InterruptedException e) { System.out.println("second Interrupted when sleep"); } } System.out.println(id "run" cnt); cnt ; if(cnt==5) { blRun=false; } } System.out.println("thread:" id "exit"); } } 注意1:当线程A执行到wait(),sleep(),join()时,抛出InterruptedException后,中断状态已经被系统复位了。 线程AThread.interrupted()返回的是false 实例2: static void interruptDemo() { Thread t1=new InterruptDemoThread(); t1.setName("t1"); Thread t2=new InterruptDemoThread(t1); t1.setName("t2"); t1.start(); t2.start(); } class InterruptDemoThread extends Thread { long id=0; static long count=0; Thread t; InterruptDemoThread() { this(null); } InterruptDemoThread(Thread t) { id=count ; this.t=t; } boolean blRun=true; public void run() { int cnt=0; while(blRun) { if(t!=null) { if(cnt==2) { t.interrupt(); if(t.isInterrupted()) System.out.println("set the thread " t.getName() " to be Interrupted"); } } else if(isInterrupted())@1 { /*Thread.interrupted();//@2 System.out.println("after interrupted(),the state of Interrupted is " isInterrupted());*/ try{ Thread.sleep(1); } catch(InterruptedException e) { System.out.println("Interrupted when sleep"); System.out.println( "Interrupted is " Thread.interrupted()); } } Thread.yield(); System.out.println(id "run" cnt); cnt ; if(cnt==5) { blRun=false; } } System.out.println("thread:" id "exit"); } } 注意1:如果线程被调用了interrupt(),那么如果此次该线程并不在wait(),sleep(),join()时, 下次执行wait(),sleep(),join()时,一样会抛出InterruptedException,当然抛出后该线程的中断状态也回被系统复位。 注意2:我们可以手动的使用Thread.interrupted()来使当前线程的中断状态系统复位(即清除中断状态) |
|