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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章