如何停止JAVA线程(转载)
来源:互联网 发布:淘宝外卖不能分口袋吗 编辑:程序博客网 时间:2024/05/17 01:07
如何停止JAVA线程
如何停止java的线程一直是一个困恼我们开发多线程程序的一个问题。这个问题最终在Java5的java.util.concurrent中得到了回答:使用interrupt(),让线程在run方法中停止。
简介
在Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(), stop(), stop(Throwable) and suspend(), destroy() and resume()。通过这些方法,我们可以对线程进行方便的操作,但是这些方法中,只有start()方法得到了保留。
在Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中详细讲解了舍弃这些方法的原因。那么,我们究竟应该如何停止线程呢?
建议使用的方法
在《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中,建议使用如下的方法来停止线程:
private volatile Thread blinker;
public void stop() {
blinker = null;
}
public void run() {
Thread thisThread = Thread.currentThread();
while (blinker == thisThread) {
try {
thisThread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
关于使用volatile关键字的原因,请查看http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#36930。
当线程处于非运行(Run)状态
当线程处于下面的状况时,属于非运行状态:
当sleep方法被调用。
当wait方法被调用。
当被I/O阻塞,可能是文件或者网络等等。
当线程处于上述的状态时,使用前面介绍的方法就不可用了。这个时候,我们可以使用interrupt()来打破阻塞的情况,如:
public void stop() { Thread tmpBlinker = blinker; blinker = null; if (tmpBlinker != null) { tmpBlinker.interrupt(); } }
当interrupt()被调用的时候,InterruptedException将被抛出,所以你可以再run方法中捕获这个异常,让线程安全退出:
try { .... wait();} catch (InterruptedException iex) { throw new RuntimeException("Interrupted",iex);}
阻塞的I/O
当线程被I/O阻塞的时候,调用interrupt()的情况是依赖与实际运行的平台的。在Solaris和Linux平台上将会抛出InterruptedIOException的异常,但是Windows上面不会有这种异常。所以,我们处理这种问题不能依靠于平台的实现。如:
package com.cnblogs.gpcusterimport java.net.*;import java.io.*;public abstract class InterruptibleReader extends Thread { private Object lock = new Object( ); private InputStream is; private boolean done; private int buflen; protected void processData(byte[] b, int n) { } class ReaderClass extends Thread { public void run( ) { byte[] b = new byte[buflen]; while (!done) { try { int n = is.read(b, 0, buflen); processData(b, n); } catch (IOException ioe) { done = true; } } synchronized(lock) { lock.notify( ); } } } public InterruptibleReader(InputStream is) { this(is, 512); } public InterruptibleReader(InputStream is, int len) { this.is = is; buflen = len; } public void run( ) { ReaderClass rc = new ReaderClass( ); synchronized(lock) { rc.start( ); while (!done) { try { lock.wait( ); } catch (InterruptedException ie) { done = true; rc.interrupt( ); try { is.close( ); } catch (IOException ioe) {} } } } }}
另外,我们也可以使用InterruptibleChannel接口。 实现了InterruptibleChannel接口的类可以在阻塞的时候抛出ClosedByInterruptException
。如:
package com.cnblogs.gpcusterimport java.io.BufferedReader;import java.io.FileDescriptor;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.nio.channels.Channels;public class InterruptInput { static BufferedReader in = new BufferedReader( new InputStreamReader( Channels.newInputStream( (new FileInputStream(FileDescriptor.in)).getChannel()))); public static void main(String args[]) { try { System.out.println("Enter lines of input (user ctrl+Z Enter to terminate):"); System.out.println("(Input thread will be interrupted in 10 sec.)"); // interrupt input in 10 sec (new TimeOut()).start(); String line = null; while ((line = in.readLine()) != null) { System.out.println("Read line:'"+line+"'"); } } catch (Exception ex) { System.out.println(ex.toString()); // printStackTrace(); } } public static class TimeOut extends Thread { int sleepTime = 10000; Thread threadToInterrupt = null; public TimeOut() { // interrupt thread that creates this TimeOut. threadToInterrupt = Thread.currentThread(); setDaemon(true); } public void run() { try { sleep(10000); // wait 10 sec } catch(InterruptedException ex) {/*ignore*/} threadToInterrupt.interrupt(); } }}
这里还需要注意一点,当线程处于写文件的状态时,调用interrupt()不会中断线程。
参考资料
How to Stop a Thread or a Task
Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?
Java Threads, Third Edition O'Reilly
- 如何停止JAVA线程(转载)
- 如何停止JAVA线程
- 如何停止JAVA线程
- 如何停止JAVA线程
- 如何停止JAVA线程
- 如何停止java线程
- 如何停止JAVA线程
- Java如何停止线程
- 如何停止java线程
- Java如何停止线程
- 如何正确停止线程(JAVA)
- 如何停止一个Java线程
- java中如何停止线程
- (六)如何停止线程?
- JAVA——如何停止线程呢?(interrupt)
- java中如何停止(stop)一个线程
- 【Java 多线程】Java 如何停止线程
- 如何正确停止java中的线程
- 响应式网页设计与应用(3)
- java虚拟机监控
- 设计模式C++实现(2)——策略模式
- 数据库基本操作
- 可变参数函数
- 如何停止JAVA线程(转载)
- 数以百万的iOS 越狱,你怎么知道你的iPhone没有被黑?
- 显示器相关的一些文章
- 在IIS中搭好网站以后 登陆网站会弹出输入服务器用户名密码解决方案
- 如何去除Oracle库表结构中的类空格字符
- 一天一个设计模式(3):观察者模式
- REST 表征状态转移
- java 类装载器
- 自定义字体