I have a thread, inside its making a database call that sometimes hangs, normally timeout should be handle in the client library, but in this case the client library doesn't handle it correctly so I have to add an extra scheduled job to cancel the thread that is making db call if it takes too long to avoid back pressure.
Right now I am terminating the thread through
final Future handler = executor.submit(new MyTask());
executor2.schedule(new Runnable(){
public void run(){
handler.cancel(true);
}
}, 3, TimeUnit.SECONDS);
Is there a proper way to interrupt a thread without TimeUnit.MILLISECONDS.sleep(1);
? Adding sleep seems hacky
@Override
public Boolean call() {
try {
// Sometimes hangs
db.call();
// Need it to be here to call InterruptedException
TimeUnit.MILLISECONDS.sleep(1);
return true;
} catch (InterruptedException e) {
return false;
}
}