Recently I am learning java concurrency and learned the source code of the LinkedBlockingQueue class. The
book says that the null parameter poll method is non-blocking:
My confusion is that the method clearly uses an exclusive lock, but why is it still non-blocking?
Explanation:
The author's blocking/non-blocking is discussed based on the characteristics of the blocking queue, not the thread characteristics. Here, non-blocking means that when the blocking queue is empty, null is returned directly, and blocking means that when the blocking queue is empty, the method blocks until there is Return after the data.
The author here says that this method is non-blocking, because
before acquiring the lock, it will make an if judgment based on the queue status, and then return. Therefore, it is non-blocking.
Let's look at the put method: we
can find that the put method does not perform if judgment on the queue attributes, and directly enters the lock operation.
Therefore, the put method is blocked.
Did you lose school?