Java并发新构件之Exchanger JDK API Exchaner 介绍 JDK API 解释 A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object on entry to the exchange method, matches with a partner thread, and receives its par…
例子很简单 Exchanger可以理解为消息队列或者说是一个通信管道,从一边拿到消息,另外一边进行消费. 不过这个是同步实现的,消费者在exchange之前,生产者一直处于等待状态,而不是一直生产. 代码如下: package exchange; import java.util.concurrent.Exchanger; public class ExchangerDemo { final static Exchanger<String> exchanger = new Exchanger&…