Code example for ReentrantLock

Methods: isHeldByCurrentThread, lock, unlock

0
    public Future<E> pollAsync() { 
        Future<E> result = null; 
        final ReentrantLock lock = this.lock; 
        boolean error = true; 
        //grab the global lock 
        lock.lock(); 
        try { 
            //check to see if we have objects in the queue 
            E item = items.poll(); 
            if (item==null) { 
                //queue is empty, add ourselves as waiters 
                ExchangeCountDownLatch<E> c = new ExchangeCountDownLatch<E>(1); 
                waiters.addLast(c); 
                lock.unlock(); 
                //return a future that will wait for the object 
                result = new ItemFuture<E>(c); 
            } else { 
                lock.unlock(); 
                //return a future with the item 
                result = new ItemFuture<E>(item); 
            } 
            error = false; 
        } finally { 
            if (error && lock.isHeldByCurrentThread()) { 
                lock.unlock(); 
            } 
        } 
        return result; 
    }