private void waitForTaskToFinish ( Future < ? > future , long timeout , TimeUnit unit ) throws Exception { try { future . get ( timeout , unit ) ; } catch ( TimeoutException e ) { throw new Exception ( "task timed out" , e ) ; } catch ( InterruptedException e ) { /* Re-assert the thread's interrupted status */ Thread . currentThread ( ) . interrupt ( ) ; if ( trace ) log . trace ( "task was interrupted" ) ; } finally { /* no-op if task is completed */ future . cancel ( true ) ; /* interrupt if running */ } }
Waits within a time constraint for a task to finish .