public Optional<T> poll(long timeout, TimeUnit timeUnit) throws InterruptedException{
        long left = timeUnit.toNanos(timeout);
        //acquire the lock on the lock object
        lock.lockInterruptibly();
        T t;
        try{
            //if count is 0 means there is no item to poll. Keep trying to poll
            //till either item is available or left gets 0 or less which means its
            //time to time out.
            while(count == 0){
                if(left <= 0){
                    return Optional.empty();
                }
                //if queue is empty wait fir signal from notEmpty condition
                left = notEmpty.awaitNanos(timeUnit.toNanos(left));
            }
            //dequeu the item.
            t = dequeue();
            //signal notFull since queue is not full anymore
            notFull.signal();
        } finally {
            //unlock the lock object
            lock.unlock();
        }
        return Optional.of(t);
    }
Poll an item from queue. If queue is empty wait with timeout till item is available
@return Optional<T> depending on if item was polled or queue was empty
src/com/interview/multithreaded/BoundedBlockingQueue.java