LinkedBlockingQueue fullyLock() throw exception, fullyUnlock() won't execute

44 Views Asked by At

fullyLock() contains 2 locks:putLock and takeLock, they are all ReentrantLock and NonfairSync

    void fullyLock() {
        putLock.lock();
        takeLock.lock();
    }

Will there be such a situation :

In remove() method, putLock.lock() success, takeLock.lock() throw exception, then the current thread will always hold putLock, other thread will never be able to operate this LinkedBlockingQueue.

  1. Is it possible that such a problem may occur?
  2. If it is not possible, why?
  3. If possible, what can jdk or us do something to remedy it?

remove() code:

        public void remove() {
            if (lastRet == null)
                throw new IllegalStateException();
            fullyLock();
            try {
                Node<E> node = lastRet;
                lastRet = null;
                for (Node<E> trail = head, p = trail.next;
                     p != null;
                     trail = p, p = p.next) {
                    if (p == node) {
                        unlink(p, trail);
                        break;
                    }
                }
            } finally {
                fullyUnlock();
            }
        }
1

There are 1 best solutions below

2
vinod On

The lock method won't throw any exceptions. Hence your issue won't occur.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html