public boolean await ( T t , long timeout , TimeUnit unit ) throws InterruptedException { synchronized ( monitor ) { // Holds the absolute time when the timeout expires. long expiryTimeNanos = System . nanoTime ( ) + unit . toNanos ( timeout ) ; // Used to hold the estimated wait time until the condition may pass. long waitNanos = evaluateWithWaitTimeNanos ( t ) ; // Loop forever until all conditions pass, the timeout expires, or the thread is interrupted. while ( waitNanos > 0 ) { // Check how much time remains until the timeout expires. long remainingTimeNanos = expiryTimeNanos - System . nanoTime ( ) ; // Check if the timeout has expired. if ( remainingTimeNanos <= 0 ) { return false ; } // If some conditions failed, then wait for the shortest of the wait time or the remaining time until the // timout expires, or until the thread is woken up by a signal, before re-evaluating conditions. long timeToPauseNanos = ( waitNanos < remainingTimeNanos ) ? waitNanos : remainingTimeNanos ; long milliPause = timeToPauseNanos / 1000000 ; int nanoPause = ( int ) ( timeToPauseNanos % 1000000 ) ; monitor . wait ( milliPause , nanoPause ) ; // Re-evelaute the condition and obtain a new estimate of how long until it may pass. waitNanos = evaluateWithWaitTimeNanos ( t ) ; } } // All conditions have passed when the above loop terminates. return true ; }
Wait for up to a timeout limit for the predicate to become true on the specified object .