Tag: 阻塞队列

我可以在Java中使用Semaphore实现阻塞队列吗?

我想知道是否可以使用Semaphore来实现阻塞队列? 在下面的代码中,我使用一个信号量来保护关键部分,并使用另外两个信号量对象来跟踪空槽和填充对象的数量。 public class BlockingQueue { private List queue = new LinkedList(); private int limit; private Semaphore slots; // semaphore for empty slots private Semaphore objs; // semaphore for filled slots private Semaphore mutex; // for the critical section public BlockingQueue(int limit) { this.limit = limit; this.slots = new Semaphore(limit); // initial empty slot = […]