In the past, I did something similar when I created an external process using Runtime.getRuntime().exec(command) . I think you could do something like this in your method:
Timer timer = new Timer(true); InterruptTimerTask interruptTimerTask = new InterruptTimerTask(Thread.currentThread()); timer.schedule(interruptTimerTask, waitTimeout); try { // put here the portion of code that may take more than "waitTimeout" } catch (InterruptedException e) { log.error("timeout exeeded); } finally { timer.cancel(); }
and here is InterruptTimerTask
protected class InterruptTimerTask extends TimerTask { private Thread theTread; public InterruptTimerTask(Thread theTread) { this.theTread = theTread; } @Override public void run() { theTread.interrupt(); } }
MarcoS
source share