您的位置:首页 > 编程语言 > Java开发

关闭超时的java线程 --利用join和interrupt方法

2009-05-26 14:32 561 查看
httpclient中就提供了这样一个class- timeoutcontroller(位于org.Apache.commons.httpclient.util包内)查看该class的源代码可知其实现细节:
public static void execute(thread task, long timeout) throws timeoutexception {
task.start();
try {
task.join(timeout);
} catch (interruptedexception e) {
/* if somebody interrupts us he knows what he is doing */
}
if (task.isalive()) {
task.interrupt();
throw new timeoutexception();
}
}
其实就是通过join()和interrupt()方法实现这种功能,文档中强调了task的interrupt()方法必须重写(override)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: