第四部分 线程的终止

本文介绍了如何通过线程的休眠和中断机制来终止线程,包括使用Thread类的stop(), join(), 和 yield() 方法,并通过示例代码演示了这些方法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章出自:http://blog.youkuaiyun.com/cuiran/article/details/6133745

对于终止运行中的线程,Thread类原本提供了一个人方法:stop();但方法已经被禁用。就目前而言,我们可以利用线程的休眠和中断机制,在子线程中有意地为调度线程安排中断机会。

下面例子就是利用线程的休眠和中断机制来终止线程。【StopThread】

[java]  view plain copy
  1. /** 
  2.  * StopThread.java 
  3.  * 版权所有(C) 2011 cuiran2001@163.com 
  4.  * 创建:崔冉  2011-1-11 下午03:36:26 
  5.  */  
  6.   
  7. package com.cayden.thread7233;  
  8.   
  9. /** 
  10.  * @author 崔冉 
  11.  * @version 1.0.0 
  12.  * @desc 
  13.  */  
  14. public class StopThread {  
  15.   
  16.     /** 
  17.      * @param args 
  18.      */  
  19.     public static void main(String[] args) throws InterruptedException{  
  20.         // TODO Auto-generated method stub  
  21.         MyThread thread1=new MyThread();  
  22.         thread1.start();  
  23.         Thread.sleep(5000);  
  24.         synchronized(thread1){  
  25.             thread1.interrupt();  
  26.         }  
  27.     }  
  28.   
  29. }  
  30. class MyThread extends Thread  
  31. {  
  32.     public void run(){  
  33.         for(int i=0;i<10000;i++){  
  34.             System.out.println("<"+i+">线程运行中...");  
  35.             try{  
  36.                 Thread.sleep(1000);  
  37.             }catch (InterruptedException e) {  
  38.                 System.out.println("线程被中止");  
  39.                 break;  
  40.             }  
  41.         }  
  42.     }  
  43. }  

但对于复杂的场景,可以根据这个模型来设计线程的终止机制。在和终止线程的方法中,join()方法来等待线程结束,join()方法并不能终止某线程。而是提供了一个阻塞当前线程,等待某线程终止的途径。对join()方法简单说明:

 void join(): 一直阻塞当前线程,等待线程结束。在等待过程中,如果遇上中断请求,则抛出InterruptedException异常。

 void join(long timeout) :在timeout指定的毫秒时间内阻塞当前线程,等待线程结束。在等待过程中,如果遇上中断请求,则抛出InterruptedException异常。

 void join(long timeout,int nanos):在timeout指定的毫秒+nanos指定的微妙时间内阻塞线程,等待线程结束。在等待过程中,如果遇上中断请求,则抛出InterruptedException异常。

代码如下:【JoinThread】

[java]  view plain copy
  1. /** 
  2.  * JoinThread.java 
  3.  * 版权所有(C) 2011 cuiran2001@163.com 
  4.  * 创建:崔冉  2011-1-11 下午04:03:40 
  5.  */  
  6.   
  7. package com.cayden.thread7233;  
  8.   
  9. /** 
  10.  * @author 崔冉 
  11.  * @version 1.0.0 
  12.  * @desc 
  13.  */  
  14. public class JoinThread {  
  15.   
  16.     /** 
  17.      * @param args 
  18.      */  
  19.     public static void main(String[] args) throws InterruptedException{  
  20.         // TODO Auto-generated method stub  
  21.         TestThread thread1=new TestThread();  
  22.         thread1.start();  
  23.         long beginTime=System.currentTimeMillis();  
  24.         thread1.join();  
  25.         long endTime=System.currentTimeMillis();  
  26.         System.out.println("等待"+(endTime-beginTime)/1000+"秒后线程终止");  
  27.     }  
  28.   
  29. }  
  30. class TestThread extends Thread  
  31. {  
  32.     public void run(){  
  33.         for(int i=0;i<10;i++){  
  34.             System.out.println("<"+i+">线程运行中...");  
  35.             try{  
  36.                 Thread.sleep(1000);  
  37.             }catch (InterruptedException e) {  
  38.                 e.printStackTrace();  
  39.                   
  40.             }  
  41.         }  
  42.     }  
  43. }  

用Thread类的yield()方法可以使线程主动让出CPU时间片,会的就绪状态。等到JVM调度器再次选中该线程。【YieldThread】

 

[java]  view plain copy
  1. /** 
  2.  * YieldThread.java 
  3.  * 版权所有(C) 2011 cuiran2001@163.com 
  4.  * 创建:崔冉  2011-1-12 上午09:03:25 
  5.  */  
  6.   
  7. package com.cayden.thread7233;  
  8.   
  9. /** 
  10.  * @author 崔冉 
  11.  * @version 1.0.0 
  12.  * @desc 
  13.  */  
  14. public class YieldThread {  
  15.   
  16.     /** 
  17.      * @param args 
  18.      */  
  19.     public static void main(String[] args) throws InterruptedException{  
  20.         // TODO Auto-generated method stub  
  21.         SleepingThread thread1=new SleepingThread(1);  
  22.         SleepingThread thread2=new SleepingThread(2);  
  23.           
  24.         thread1.start();  
  25.         thread2.start();  
  26.           
  27.         Thread.sleep(5000);  
  28.         thread1.yield();  
  29.           
  30.         if(thread1.isAlive()){  
  31.             System.out.println("线程1仍然存活");  
  32.         }  
  33.           
  34.     }  
  35.   
  36. }  
  37. class SleepingThread extends Thread  
  38. {  
  39.     private int no=0;  
  40.     public SleepingThread(int no){  
  41.         this.no=no;  
  42.     }  
  43.     public void run(){  
  44.         for(int i=0;i<10;i++){  
  45.             System.out.println("<"+i+">"+this.no+"线程运行中...");  
  46.             try{  
  47.                 Thread.sleep(1000);  
  48.             }catch (InterruptedException e) {  
  49.                 // TODO: handle exception  
  50.                 e.printStackTrace();  
  51.             }  
  52.         }  
  53.     }  
  54. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值