java - 自定义消息传递信号量?

标签 java multithreading data-structures concurrency synchronization

假设我有两个主要运行的线程 A 和 B,以及一个异步调用的线程 T。我希望线程 A 等待直到线程 T 收到消息 aMsg,而线程 B 停止直到线程收到消息 msgB T. 我知道如何使用 2 个信号量来做到这一点:

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)

最佳答案

伟大的直觉 m3th0dman。我想你要找的是 Transfer Queue .

这是“一个 BlockingQueue,生产者可以在其中等待消费者接收 元素”。在你的例子中,线程 A 和 B 是生产者,而线程 T 是唯一的 消费者。

简而言之:

  1. 创建共享 TransferQueue<Object> synchronizer = new TransferQueue<>();
  2. 线程 A 和 B 调用阻塞方法 synchronizer.transfer(new Object());
  3. 与此同时,线程 t 调用 synchronizer.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();
    }
}

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

关于java - 自定义消息传递信号量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19317492/

相关文章:

python - 在 O(ln n) 的有序序列中插入一个值

c - 显示功能不打印链接列表的值

c# - 如何从另一个类更新 Windows 窗体 GUI?

android - 如何在 Kotlin (Android) 中等待 firebase 调用结束

java - 在 java 8 中将核心池大小设置为 0 的有效用例是什么?

java - 使用 java.util.concurrent.Executor 阻止 tomcat 停止

algorithm - 查找树中间附近的所有节点

java - Android key 对生成格式为 -----BEGIN PUBLIC KEY----- 并以 -----END PUBLIC KEY----- 结尾

java - servlet 中无效的 sql 语句

java - 如何完全测试内部具有 System.getenv ("name") 操作的构造函数的覆盖率