【问题标题】:Custom message passing semaphore?自定义消息传递信号量?
【发布时间】:2013-10-11 11:52:29
【问题描述】:

假设我有两个主要运行的线程 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)

【问题讨论】:

  • 我不知道。您可以使用Lock 和多个Conditions,这样会更清晰但不清晰。

标签: java multithreading data-structures concurrency synchronization


【解决方案1】:

伟大的直觉 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();
    }
}

我希望嵌套类和匿名类不会让人分心。

【讨论】:

  • 其实不需要每次都创建new Object()。重复使用同一个即可。
猜你喜欢
  • 2012-06-12
  • 2013-08-05
  • 1970-01-01
  • 2015-08-14
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
相关资源
最近更新 更多