A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© jix707 初级黑马   /  2013-8-13 14:56  /  1003 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  • class Demo implements Runnable
  • {
  • public synchronized void run()
  • {
  • try
  • {
  • wait();
  • }
  • catch (InterruptedException e)
  • {
  • }
  • for (int x = 0;x<70 ;x++ )
  • {
  • System.out.println(Thread.currentThread().getName()+"..."+x);
  • }
  • }
  • }
  • class JoinDemo
  • {
  • public static void main(String[] args) throws Exception
  • {
  • Demo d = new Demo();
  • Thread t1 = new Thread(d);
  • Thread t2 = new Thread(d);
  • t1.start();
  • //t2.start();
  • t1.join();
  • //t1.interrupt();
  • //t2.start();
  • for (int x = 0;x<80 ;x++ )
  • {
  • System.out.println("main..."+x);
  • }
  • System.out.println("Over!");
  • }
  • }

1 个回复

倒序浏览
在JDK1.0中,可以用stop方法来终止,但是现在这种方法已经被禁用了,改用interrupt方法。

Thread.interrupt()方法不会中断一个正在运行的线程。它的作用是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。

interrupt方法并不是强制终止线程,它只能设置线程的interrupted状态,而在线程中一般使用一下方式:
while (!Thread.currentThread().isInterrupted() && more work to do)

{...}

而被block的线程(sleep() or join())在被调用interrupt时会产生InterruptException,此时是否终止线程由本线程自己决定。程序的一般形式是:
public void run()
{
try
{
. . .
while (!Thread.currentThread().isInterrupted() && more work to do)
{
do more work
}
}
catch(InterruptedException e)
{
// thread was interrupted during sleep or wait
}
finally
{
cleanup, if required
}
// exiting the run method terminates the thread
}

Thread.sleep方法也会产生InterruptedException,因此,如果每次在做完一些工作后调用了sleep方法,那么就不用检查isInterrupted,而是直接捕捉InterruptedException。

---------------------------------------------------------------------------------------

假如我们有一个任务如下,交给一个Java线程来执行,如何才能保证调用interrupt()来中断它呢?

class ATask implements Runnable{  
   
     private double d = 0.0;  
      
     public void run() {  
         //死循环执行打印"I am running!" 和做消耗时间的浮点计算  
         while (true) {  
             System.out.println("I am running!");  
               
             for (int i = 0; i < 900000; i++) {  
                 d =  d + (Math.PI + Math.E) / d;  
             }  
             //给线程调度器可以切换到其它进程的信号  
             Thread.yield();  
         }  
     }  
}  
   
public class InterruptTaskTest {  
      
     public static void main(String[] args) throws Exception{  
         //将任务交给一个线程执行  
         Thread t = new Thread(new ATask());  
         t.start();  
           
        //运行一断时间中断线程  
         Thread.sleep(100);  
         System.out.println("****************************");  
         System.out.println("Interrupted Thread!");  
         System.out.println("****************************");  
         t.interrupt();  
     }  
}   


运行这个程序,我们发现调用interrupt()后,程序仍在运行,如果不强制结束,程序将一直运行下去,如下所示:
......  
I am running!  
I am running!  
I am running!  
I am running!  
****************************  
Interrupted Thread!  
****************************  
I am running!  
I am running!  
I am running!  
I am running!  
I am running!  
....  

虽然中断发生了,但线程仍然在进行,离开线程有两种常用的方法:
抛出InterruptedException和用Thread.interrupted()检查是否发生中断,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马