Java 自定义消息传递信号量?

Java 自定义消息传递信号量?,java,multithreading,data-structures,concurrency,synchronization,Java,Multithreading,Data Structures,Concurrency,Synchronization,假设我有两个正在运行的主线程A和B,以及一个异步调用的线程T。我希望线程A等待,直到线程T上收到消息aMsg,线程B停止,直到线程T上收到消息msgB。我知道如何使用2个sempahores执行此操作: sempahoreA = new Sempahore(0); sempahoreB = new Sempahore(0); //in thread A //code until where to run semaphoreA.acquire() //in thread B //code u

假设我有两个正在运行的主线程A和B,以及一个异步调用的线程T。我希望线程A等待,直到线程T上收到消息aMsg,线程B停止,直到线程T上收到消息msgB。我知道如何使用2个sempahores执行此操作:

sempahoreA = new Sempahore(0);
sempahoreB = new Sempahore(0);

//in thread A
//code until where to run 
semaphoreA.acquire()

//in thread B
//code until where to run 
semaphoreB.acquire()

//in thread T
if (msgA.equals(msgRecevied)) {
    semaphoreA.release()
} 
if (msgB.equals(msgReceived)) {
    semaphoreB.release()
}
问题是我有多个A,B,C,。。。线程和我不想使用多个信号量。
java.util.concurrent
中是否有一个类可以用一个实例替换所有信号量

synchronizer = //?

//in thread A
//code until where to run 
synchronizer.acquire(msgA)//only let go if msgA is received from thread calling release

//in thread B
//code until where to run 
synchronizer.acquire(msgB)//only let go if msgA is received from thread calling release

//in thread T
if (msgA.equals(msgRecevied)) {
    synchronizer.release(msgA)
} 
if (msgB.equals(msgReceived)) {
    synchronizer.release(msgB)
}//actually here you can call synchronizer.release(msgReceived)

伟大的直觉。我想你要找的是一个

这是一个“封锁队列”,生产者可以在其中等待消费者接收 要素”。在您的例子中,线程A和B是生产者,线程T是唯一的生产者 消费者

简言之:

  • 创建一个共享的
    TransferQueue同步器=新建TransferQueue()
  • 线程A和B调用阻塞方法
    synchronizer.transfer(newobject())
  • 同时,线程t调用同步器.take()在空闲时解锁
  • 下面是一个例子:

    import java.util.concurrent.TransferQueue;
    
    public class TransferQueueExample {
        public static void main(String[] args) {
            final TransferQueue<Object> synchronizer = new TransferQueue<>();
    
            for (int i = 0; i < 10; i++) {
                final Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // Do work until
                        // ...
                        System.out.println("Thread " + i + " is transferring.");
    
                        synchronizer.transfer(new Object()); // This is blocking
                        System.out.println("Thread " + i + " done transferring.");
                    }
                }).start();
            }
    
            final Thread consumer = new Thread(new Runnable() {
                @Override
                public void run() {
                    // TODO: Note thatthis will run indefinitely so in a real
                    // scenario, find a way of shutting this thread down.
                    while (true) {
                        System.out.println("There are about " +
                                           synchronizer.getWaitingConsumerCount()
                                           + " threads waiting");
                        synchronizer.take();
                        System.sleep(1000);
                    }
                }
            }).start();
        }
    }
    
    import java.util.concurrent.TransferQueue;
    公共类TransferQueueExample{
    公共静态void main(字符串[]args){
    最终TransferQueue同步器=新的TransferQueue();
    对于(int i=0;i<10;i++){
    最终线程t=新线程(新可运行(){
    @凌驾
    公开募捐{
    //工作到
    // ...
    System.out.println(“线程”+i+“正在传输”);
    synchronizer.transfer(new Object());//这是阻塞
    System.out.println(“线程”+i+“完成传输”);
    }
    }).start();
    }
    最终线程使用者=新线程(新可运行(){
    @凌驾
    公开募捐{
    //TODO:请注意,这将无限期运行,因此在实际应用中
    //在场景中,找到关闭此线程的方法。
    while(true){
    System.out.println(“大约有”+
    synchronizer.getWaitingConsumerCount()同步器
    +“线程等待”);
    同步器;
    系统睡眠(1000);
    }
    }
    }).start();
    }
    }
    

    我希望嵌套类和匿名类不会太分散注意力。

    据我所知不是这样。您可以使用带有多个锁的
    锁,这会更清晰,但不会更清晰。实际上,您不需要每次都创建一个
    新对象()。重复使用同一个。