public E poll ( long timeout , TimeUnit unit ) throws InterruptedException { ReentrantLock lock = this . lock ; lock . lockInterruptibly ( ) ; try { long nanos = unit . toNanos ( timeout ) ; do { if ( count != 0 ) { return extract ( true , true ) . getElement ( ) ; } try { nanos = notEmpty . awaitNanos ( nanos ) ; } catch ( InterruptedException ie ) { notEmpty . signal ( ) ; // propagate to non-interrupted thread throw ie ; } } while ( nanos > 0 ) ; return null ; } finally { lock . unlock ( ) ; } }
Retrieves and removes the head of this queue waiting if necessary up to the specified wait time if no elements are present on this queue .