中斷(Interrupt)一個線程意味着在該線程完成任務之前停止其正在進行的一切,有效地中止其當前的操作。線程是死亡、還是等待新的任務或是繼續運行至下一步,就取決於這個程序。雖然初次看來它可能顯得簡單,但是,你必須進行一些預警以實現期望的結果。你最好還是牢記以下的幾點告誡。
首先,忘掉Thread.stop方法。雖然它確實停止了一個正在運行的線程,然而,這種方法是不安全也是不受提倡的,這意味着,在未來的JAVA版本中,它將不復存在。
1:
- public static void main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- //線程沒有處於阻塞狀態,調用線程對應的interrupt()不能讓運行的線程停止下來
- t.interrupt();
- }
- static class MyThread implements Runnable {
- public volatile boolean stop = false;
- private void dosomethig() throws InterruptedException {
- long time = System.currentTimeMillis();
- while(System.currentTimeMillis() - time < 1000) {
- }
- System.out.println("all things had been done!!");
- }
- @Override
- public void run() {
- try {
- while(!stop) {
- System.out.println(Thread.currentThread().getName() + " is running..");
- dosomethig();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- System.out.println(Thread.currentThread().getName() + " is exiting under request.");
- }
- }
- }
- 運行結果:
- System is ready to start thread
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- System is ready to stop thread
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
2:
- public static void main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- // t.interrupt();
- //當線程沒有處於阻塞狀態,通過改變標誌量,可以讓線程停止運行
- mt.stop = true;
- }
- 運行結果:
- System is ready to start thread
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- System is ready to stop thread
- all things had been done!!
- Thread-0 is exiting under request.
3:
- public static void main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- // t.interrupt();
- //此時線程一直處於阻塞狀態,無法檢查標誌量,所以僅通過改變標誌量無法停止線程
- mt.stop = true;
- }
- static class MyThread implements Runnable {
- public volatile boolean stop = false;
- private void dosomethig() throws InterruptedException {
- // long time = System.currentTimeMillis();
- // while(System.currentTimeMillis() - time < 1000) {
- //
- // }
- Thread.currentThread().join();
- System.out.println("all things had been done!!");
- }
- @Override
- public void run() {
- try {
- while(!stop) {
- System.out.println(Thread.currentThread().getName() + " is running..");
- dosomethig();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- System.out.println(Thread.currentThread().getName() + " is exiting under request.");
- }
- }
- }
- 運行結果:
- System is ready to start thread
- Thread-0 is running..
- System is ready to stop thread
4:
- public static void main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- //通過調用線程對象上的interrupt() 正在運行的線程對象會接收到一個InterruptedException異常,從而停止運行
- t.interrupt();
- // mt.stop = true;
- }
- 運行結果:
- System is ready to start thread
- Thread-0 is running..
- System is ready to stop thread
- java.lang.InterruptedException
- Thread-0 is exiting under request.
- at java.lang.Object.wait(Native Method)
- at java.lang.Thread.join(Thread.java:1143)
- at java.lang.Thread.join(Thread.java:1196)
- at com.thread.DeadLockTest$MyThread.dosomethig(DeadLockTest.java:29)
- at com.thread.DeadLockTest$MyThread.run(DeadLockTest.java:38)
- at java.lang.Thread.run(Thread.java:619)
5:中斷I/O操作
- public static void main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- t.interrupt();
- mt.stop = true;
- mt.socket.close();
- }
- static class MyThread implements Runnable {
- public volatile boolean stop = false;
- ServerSocket socket = null;
- private void dosomethig() throws InterruptedException, IOException {
- // long time = System.currentTimeMillis();
- // while(System.currentTimeMillis() - time < 1000) {
- //
- // }
- // Thread.currentThread().join();
- socket = new ServerSocket(9999);
- //這裏需要調用Socket對應的close方法 這樣子 被阻塞的線程會接收到一個SocketException 從而停止運行
- socket.accept();
- System.out.println("all things had been done!!");
- }
- @Override
- public void run() {
- try {
- while(!stop) {
- System.out.println(Thread.currentThread().getName() + " is running..");
- dosomethig();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- System.out.println(Thread.currentThread().getName() + " is exiting under request.");
- }
- }
- }
- 運行結果:
- System is ready to start thread
- Thread-0 is running..
- System is ready to stop thread
- java.net.SocketException: socket closed
- Thread-0 is exiting under request.
- at java.net.PlainSocketImpl.socketAccept(Native Method)
- at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
- at java.net.ServerSocket.implAccept(ServerSocket.java:453)
- at java.net.ServerSocket.accept(ServerSocket.java:421)
- at com.thread.DeadLockTest$MyThread.dosomethig(DeadLockTest.java:33)
- at com.thread.DeadLockTest$MyThread.run(DeadLockTest.java:42)
- at java.lang.Thread.run(Thread.java:619)
參考》
http://www.blogjava.net/jinfeng_wang/archive/2008/04/27/196477.html