java 关闭一个正在执行的线程(转)

 

本文转自:http://blog.csdn.net/witsmakemen/article/details/12580741

 

 

中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切,有效地中止其当前的操作。线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序。虽然初次看来它可能显得简单,但是,你必须进行一些预警以实现期望的结果。你最好还是牢记以下的几点告诫。

首先,忘掉Thread.stop方法。虽然它确实停止了一个正在运行的线程,然而,这种方法是不安全也是不受提倡的,这意味着,在未来的JAVA版本中,它将不复存在。

1:

  1. public static void main(String[] args) throws Exception {  
  2.         MyThread mt = new MyThread();  
  3.         Thread t = new Thread(mt);  
  4.         System.out.println("System is ready to start thread");  
  5.         t.start();  
  6.           
  7.         Thread.sleep(3000);  
  8.           
  9.         System.out.println("System is ready to stop thread");  
  10.         //线程没有处于阻塞状态,调用线程对应的interrupt()不能让运行的线程停止下来  
  11.         t.interrupt();  
  12.     }  
  13.       
  14.     static class MyThread implements Runnable {  
  15.           
  16.         public volatile boolean stop = false;  
  17.           
  18.         private void dosomethig() throws InterruptedException {  
  19.             long time = System.currentTimeMillis();  
  20.             while(System.currentTimeMillis() - time < 1000) {  
  21.                   
  22.             }  
  23.             System.out.println("all things had been done!!");  
  24.         }  
  25.           
  26.         @Override 
  27.         public void run() {  
  28.             try {  
  29.                 while(!stop) {  
  30.                     System.out.println(Thread.currentThread().getName() + " is running..");  
  31.                     dosomethig();  
  32.                 }  
  33.             } catch (InterruptedException e) {  
  34.                 e.printStackTrace();  
  35.             } finally {  
  36.                 System.out.println(Thread.currentThread().getName() + " is exiting under request.");  
  37.             }  
  38.         }  
  39.     } 
  1. 运行结果:  
  2. System is ready to start thread  
  3. Thread-0 is running..  
  4. all things had been done!!  
  5. Thread-0 is running..  
  6. all things had been done!!  
  7. Thread-0 is running..  
  8. all things had been done!!  
  9. Thread-0 is running..  
  10. System is ready to stop thread  
  11. all things had been done!!  
  12. Thread-0 is running..  
  13. all things had been done!!  
  14. Thread-0 is running..  
  15. all things had been done!!  
  16. Thread-0 is running..  
  17. all things had been done!!  
  18. Thread-0 is running..  

 

 

2:

  1. public static void main(String[] args) throws Exception {  
  2.         MyThread mt = new MyThread();  
  3.         Thread t = new Thread(mt);  
  4.         System.out.println("System is ready to start thread");  
  5.         t.start();  
  6.           
  7.         Thread.sleep(3000);  
  8.           
  9.         System.out.println("System is ready to stop thread");  
  10. //      t.interrupt();  
  11.         //当线程没有处于阻塞状态,通过改变标志量,可以让线程停止运行  
  12.         mt.stop = true;  
  13.     } 
  1. 运行结果:  
  2. System is ready to start thread  
  3. Thread-0 is running..  
  4. all things had been done!!  
  5. Thread-0 is running..  
  6. all things had been done!!  
  7. Thread-0 is running..  
  8. System is ready to stop thread  
  9. all things had been done!!  
  10. Thread-0 is exiting under request. 

 

3:

  1. public static void main(String[] args) throws Exception {  
  2.         MyThread mt = new MyThread();  
  3.         Thread t = new Thread(mt);  
  4.         System.out.println("System is ready to start thread");  
  5.         t.start();  
  6.           
  7.         Thread.sleep(3000);  
  8.           
  9.         System.out.println("System is ready to stop thread");  
  10. //      t.interrupt();  
  11. //此时线程一直处于阻塞状态,无法检查标志量,所以仅通过改变标志量无法停止线程  
  12.         mt.stop = true;  
  13.     }  
  14.       
  15.     static class MyThread implements Runnable {  
  16.           
  17.         public volatile boolean stop = false;  
  18.           
  19.         private void dosomethig() throws InterruptedException {  
  20. //          long time = System.currentTimeMillis();  
  21. //          while(System.currentTimeMillis() - time < 1000) {  
  22. //                
  23. //          }  
  24.             Thread.currentThread().join();  
  25.             System.out.println("all things had been done!!");  
  26.         }  
  27.           
  28.         @Override 
  29.         public void run() {  
  30.             try {  
  31.                 while(!stop) {  
  32.                     System.out.println(Thread.currentThread().getName() + " is running..");  
  33.                     dosomethig();  
  34.                 }  
  35.             } catch (InterruptedException e) {  
  36.                 e.printStackTrace();  
  37.             } finally {  
  38.                 System.out.println(Thread.currentThread().getName() + " is exiting under request.");  
  39.             }  
  40.         }  
  41.     } 
  1. 运行结果:  
  2. System is ready to start thread  
  3. Thread-0 is running..  
  4. System is ready to stop thread  

4:

  1. public static void main(String[] args) throws Exception {  
  2.         MyThread mt = new MyThread();  
  3.         Thread t = new Thread(mt);  
  4.         System.out.println("System is ready to start thread");  
  5.         t.start();  
  6.           
  7.         Thread.sleep(3000);  
  8.           
  9.         System.out.println("System is ready to stop thread");  
  10.         //通过调用线程对象上的interrupt() 正在运行的线程对象会接收到一个InterruptedException异常,从而停止运行  
  11.                   t.interrupt();  
  12. //      mt.stop = true;  
  13.     } 
  1. 运行结果:  
  2. System is ready to start thread  
  3. Thread-0 is running..  
  4. System is ready to stop thread  
  5. java.lang.InterruptedException  
  6. Thread-0 is exiting under request.  
  7.     at java.lang.Object.wait(Native Method)  
  8.     at java.lang.Thread.join(Thread.java:1143)  
  9.     at java.lang.Thread.join(Thread.java:1196)  
  10.     at com.thread.DeadLockTest$MyThread.dosomethig(DeadLockTest.java:29)  
  11.     at com.thread.DeadLockTest$MyThread.run(DeadLockTest.java:38)  
  12.     at java.lang.Thread.run(Thread.java:619

 

5:中断I/O操作

  1. public static void main(String[] args) throws Exception {  
  2.         MyThread mt = new MyThread();  
  3.         Thread t = new Thread(mt);  
  4.         System.out.println("System is ready to start thread");  
  5.         t.start();  
  6.           
  7.         Thread.sleep(3000);  
  8.           
  9.         System.out.println("System is ready to stop thread");  
  10.         t.interrupt();  
  11.         mt.stop = true;  
  12.         mt.socket.close();  
  13.     }  
  14.       
  15.     static class MyThread implements Runnable {  
  16.         public volatile boolean stop = false;  
  17.         ServerSocket socket = null;  
  18.           
  19.         private void dosomethig() throws InterruptedException, IOException {  
  20. //          long time = System.currentTimeMillis();  
  21. //          while(System.currentTimeMillis() - time < 1000) {  
  22. //                
  23. //          }  
  24. //          Thread.currentThread().join();  
  25.             socket = new ServerSocket(9999);  
  26.             //这里需要调用Socket对应的close方法 这样子 被阻塞的线程会接收到一个SocketException 从而停止运行  
  27.             socket.accept();  
  28.             System.out.println("all things had been done!!");  
  29.         }  
  30.           
  31.         @Override 
  32.         public void run() {  
  33.             try {  
  34.                 while(!stop) {  
  35.                     System.out.println(Thread.currentThread().getName() + " is running..");  
  36.                     dosomethig();  
  37.                 }  
  38.             } catch (InterruptedException e) {  
  39.                 e.printStackTrace();  
  40.             } catch (IOException e) {  
  41.                 e.printStackTrace();  
  42.             } finally {  
  43.                 System.out.println(Thread.currentThread().getName() + " is exiting under request.");  
  44.             }  
  45.         }  
  46.     } 
  1. 运行结果:  
  2. System is ready to start thread  
  3. Thread-0 is running..  
  4. System is ready to stop thread  
  5. java.net.SocketException: socket closed  
  6. Thread-0 is exiting under request.  
  7.     at java.net.PlainSocketImpl.socketAccept(Native Method)  
  8.     at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)  
  9.     at java.net.ServerSocket.implAccept(ServerSocket.java:453)  
  10.     at java.net.ServerSocket.accept(ServerSocket.java:421)  
  11.     at com.thread.DeadLockTest$MyThread.dosomethig(DeadLockTest.java:33)  
  12.     at com.thread.DeadLockTest$MyThread.run(DeadLockTest.java:42)  
  13.     at java.lang.Thread.run(Thread.java:619)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值