Yes, BlockingQueue
methods add()
and take()
are thread safe but with a difference.
add ()
and take()
method uses 2 different ReentrantLock
objects.
add(
) method uses
private final ReentrantLock putLock = new ReentrantLock();
take()
method uses
private final ReentrantLock takeLock = new ReentrantLock();
Hence, simultaneous access to add()
method is synchronized. Similarly, simultaneous access to take()
method is synchronized
.
But, simultaneous access to add()
and take()
method is not synchronized
since they are using 2 different lock objects (except during edge condition of queue full / empty).