public boolean addIfOpen ( E element ) { requireNonNull ( element ) ; lock . lock ( ) ; try { if ( open ) { elements . addLast ( element ) ; if ( elements . size ( ) == 1 ) { nonEmpty . signalAll ( ) ; } } return open ; } finally { lock . unlock ( ) ; } }
Tries to add an element to the queue if the queue is still open . Checking whether the queue is open and adding the element is one atomic operation .