
Originally Posted by
aravind_6776
hi,
thanks for your help. i tried this but it seems, it blocks all the threads to wait till the timeout period even if it successfully complete the processing with in timeout period set.
i need to apply the timeout only to those threads which exceed the limit. I m trying to make sure if any thread is hung due to any reason, it must be killed or timeout after x mins.
is it possible to achieve this?
Aravind
en, post the code for you
Code:
// create an timeout thread
public class TimeoutRunnable implements Runnable {
private long limitTime;
private Runnable runnable;
privaate ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 601, TimeUnit.MINITUE, new LinkedBlockQueue());
public TimeoutRunnable(long limitTime, Runnable runnable) {
this.limitTime = limitTime;
this.runnable = runnable;
}
public void run() {
executor.excutor(runable);
if(executor.awaitTerminate(....)) {
throw new TimeoutException();
}
executor.shutDown();
}
}
put your runnable implements into this class, DO NOT FORGET INVOKE SHUTDOWN