我需要调用一个外部方法,该方法有时需要不确定的时间才能完成。
void doMyStuff(){
// do my stuff
// work is being done by doStuff thread
externalMethod(); // This is external and sometimes takes hours
// I need to check if current thread is interrupted and then return after a certain time
}
然而,我负担不起线程的占用,所以我需要在固定时间后终止进程。在Websphere的WorkManagerneneneba API的帮助下,我可以从外部线程中断doStuff线程。但是只有发送中断信号是没有用的,除非被中断的线程处理它
无论extenalMethod()
是否返回,我都希望在一定时间后将doStuff线程返回到线程池。如何做到这一点?
使用myThread.isInterrupted()
查看线程是否中断。
void doMyStuffs(){
//do my stuffs
System.out.println(myThread.isInterrupted());
}