Tag: 量信号量

Java:除了它们所属的对象之外,同步方法是否锁定了什么?

现在,我不确定这是否是一个愚蠢的问题,如果是的话,请耐心等待。 对象上的锁是“递归的”,即如果两个对象在其字段中引用了第三个对象,并且一个线程正在其中一个上运行同步方法,那么任何其他线程是否可以访问第三个对象? // a and b are some objects that implement Runnable // they both reference the same third object a.ref = c; b.ref = c; // a is run in a thread and processes some data in a loop for a long time // the method the loop belongs to is declared synchronized threadA […]

我可以在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 = […]