0

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 Answers1

0

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

vinod
  • 151
  • 7
  • Doesn't it throw a RunTimeException? ReentrantLock lock() will call NonfairSync lock(), this is code: ` final void lock() { if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); } ` – jianjian Oct 15 '21 at 07:32
  • @jianjian There are no `throw` statements in that code. What are you talking about? – user207421 Oct 15 '21 at 08:59