java 停止一个正在运行的线程

中断(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. Thread.sleep(3000);
  7. System.out.println("System is ready to stop thread");
  8. //线程没有处于阻塞状态,调用线程对应的interrupt()不能让运行的线程停止下来
  9. t.interrupt();
  10. }
  11. static class MyThread implements Runnable {
  12. public volatile boolean stop = false;
  13. private void dosomethig() throws InterruptedException {
  14. long time = System.currentTimeMillis();
  15. while(System.currentTimeMillis() - time < 1000) {
  16. }
  17. System.out.println("all things had been done!!");
  18. }
  19. @Override
  20. public void run() {
  21. try {
  22. while(!stop) {
  23. System.out.println(Thread.currentThread().getName() + " is running..");
  24. dosomethig();
  25. }
  26. } catch (InterruptedException e) {
  27. e.printStackTrace();
  28. } finally {
  29. System.out.println(Thread.currentThread().getName() + " is exiting under request.");
  30. }
  31. }
  32. }
  
  
  
  
  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. Thread.sleep(3000);
  7. System.out.println("System is ready to stop thread");
  8. // t.interrupt();
  9. //当线程没有处于阻塞状态,通过改变标志量,可以让线程停止运行
  10. mt.stop = true;
  11. }
  
  
  
  
  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. Thread.sleep(3000);
  7. System.out.println("System is ready to stop thread");
  8. // t.interrupt();
  9. //此时线程一直处于阻塞状态,无法检查标志量,所以仅通过改变标志量无法停止线程
  10. mt.stop = true;
  11. }
  12. static class MyThread implements Runnable {
  13. public volatile boolean stop = false;
  14. private void dosomethig() throws InterruptedException {
  15. // long time = System.currentTimeMillis();
  16. // while(System.currentTimeMillis() - time < 1000) {
  17. //
  18. // }
  19. Thread.currentThread().join();
  20. System.out.println("all things had been done!!");
  21. }
  22. @Override
  23. public void run() {
  24. try {
  25. while(!stop) {
  26. System.out.println(Thread.currentThread().getName() + " is running..");
  27. dosomethig();
  28. }
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. } finally {
  32. System.out.println(Thread.currentThread().getName() + " is exiting under request.");
  33. }
  34. }
  35. }
  
  
  
  
  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. Thread.sleep(3000);
  7. System.out.println("System is ready to stop thread");
  8. //通过调用线程对象上的interrupt() 正在运行的线程对象会接收到一个InterruptedException异常,从而停止运行
  9. t.interrupt();
  10. // mt.stop = true;
  11. }
  
  
  
  
  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. Thread.sleep(3000);
  7. System.out.println("System is ready to stop thread");
  8. t.interrupt();
  9. mt.stop = true;
  10. mt.socket.close();
  11. }
  12. static class MyThread implements Runnable {
  13. public volatile boolean stop = false;
  14. ServerSocket socket = null;
  15. private void dosomethig() throws InterruptedException, IOException {
  16. // long time = System.currentTimeMillis();
  17. // while(System.currentTimeMillis() - time < 1000) {
  18. //
  19. // }
  20. // Thread.currentThread().join();
  21. socket = new ServerSocket(9999);
  22. //这里需要调用Socket对应的close方法 这样子 被阻塞的线程会接收到一个SocketException 从而停止运行
  23. socket.accept();
  24. System.out.println("all things had been done!!");
  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. } catch (IOException 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
  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)

 

 

参考》

http://www.blogjava.net/jinfeng_wang/archive/2008/04/27/196477.html

你可能感兴趣的:(java,thread)