1

LinkedBlockingQueue uses two separate ReentrantLocks, putLock and takeLock. This enables one reader and one writer to access the queue at the same time using the following methods:

1) poll()
2) take()
3) put()
4) offer()

Lets say a linkedBlockingQueue has only one element. poll()/take() uses takeLock and put()/offer() uses putLock. I see there will be a race condition when poll() tries to remove the only element and put() tries to add another element because one reader and one writer thread will be accessing node datastructure at the same time.

So, does this mean LinkedBlockingQueue is not completely thread-safe?

Kans
  • 382
  • 3
  • 17
  • 5
    there is package-private method `fullyLock()` that ensure no race condition when necessary (eg. called from `remove`, `contains`, `clear` , ...) `So, does this mean LinkedBlockingQueue is not completely thread-safe` - unless mentioned in javadocs collections from `java.util.concurrent` are thread-safe – rkosegi May 04 '19 at 18:04

0 Answers0