Can I implement blocking queue using Semaphore in Java?

前端 未结 2 833
孤街浪徒
孤街浪徒 2021-01-15 15:11

I wonder if it is possible to use Semaphore to implement blocking queue?

In the below codes, I use one Semaphore to protect the critical section, and two more Semaph

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 15:54

    Without testing, I would say this works. However, every release() will notify the thread blocked in acquire(). So you really have at least the same cost as a reentrantlock+condition, likely worse because there is 2 acquire and 2 release() calls.

提交回复
热议问题