中断(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