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)
关闭超时的java线程 --利用join和interrupt方法
本文介绍HTTPClient中用于实现请求超时控制的TimeoutController类。该类通过Thread的join和interrupt方法来达到指定时间内未完成任务则中断的效果,并强调了需要重写任务的interrupt方法。

被折叠的 条评论
为什么被折叠?



