Most visited

Recently visited

Added in API level 1

Exchanger

public class Exchanger
extends Object

java.lang.Object
   ↳ java.util.concurrent.Exchanger<V>


一个同步点,线程可以在其中配对和交换对内的元素。 每个线程在进入exchange方法时都会呈现一些对象,与合作伙伴线程匹配,并在返回时接收其伙伴的对象。 交换者可被视为SynchronousQueue的双向形式。 交换器可能在遗传算法和流水线设计等应用中很有用。

示例用法:以下是使用 Exchanger在线程之间交换缓冲区的类的高亮部分,以便填充缓冲区的线程在需要时获取新清空的线程,并将填充的线程交给清空缓冲区的线程。

 class FillAndEmpty {
   Exchanger<DataBuffer> exchanger = new Exchanger<>();
   DataBuffer initialEmptyBuffer = ... a made-up type
   DataBuffer initialFullBuffer = ...

   class FillingLoop implements Runnable {
     public void run() {
       DataBuffer currentBuffer = initialEmptyBuffer;
       try {
         while (currentBuffer != null) {
           addToBuffer(currentBuffer);
           if (currentBuffer.isFull())
             currentBuffer = exchanger.exchange(currentBuffer);
         }
       } catch (InterruptedException ex) { ... handle ... }
     }
   }

   class EmptyingLoop implements Runnable {
     public void run() {
       DataBuffer currentBuffer = initialFullBuffer;
       try {
         while (currentBuffer != null) {
           takeFromBuffer(currentBuffer);
           if (currentBuffer.isEmpty())
             currentBuffer = exchanger.exchange(currentBuffer);
         }
       } catch (InterruptedException ex) { ... handle ...}
     }
   }

   void start() {
     new Thread(new FillingLoop()).start();
     new Thread(new EmptyingLoop()).start();
   }
 }

内存一致性效果:对于通过 Exchanger成功交换对象的每对线程,每个线程中 exchange()之前的操作 exchange()从另一个线程中对应的 exchange()返回之后的操作。

Summary

Public constructors

Exchanger()

创建一个新的Exchanger。

Public methods

V exchange(V x, long timeout, TimeUnit unit)

等待另一个线程到达此交换点(除非当前线程为 interrupted或经过了指定的等待时间),然后将给定对象传递给它,并接收其对象。

V exchange(V x)

等待另一个线程到达此交换点(除非当前线程为 interrupted ),然后将给定对象传送给它,接收它的对象作为回报。

Inherited methods

From class java.lang.Object

Public constructors

Exchanger

Added in API level 1
Exchanger ()

创建一个新的Exchanger。

Public methods

exchange

Added in API level 1
V exchange (V x, 
                long timeout, 
                TimeUnit unit)

等待另一个线程到达此交换点(除非当前线程为 interrupted或经过指定的等待时间),然后将给定对象传送给它,并接收其对象。

如果另一个线程已经在交换点等待,那么为了线程调度目的它将被恢复,并接收当前线程传入的对象。 当前线程立即返回,接收由另一个线程传递给交换机的对象。

如果没有其他线程已经在交换机上等待,那么为了线程调度目的,当前线程被禁用,并且处于休眠状态,直到发生以下三件事之一:

  • Some other thread enters the exchange; or
  • Some other thread interrupts the current thread; or
  • The specified waiting time elapses.

如果当前线程:

  • has its interrupted status set on entry to this method; or
  • is interrupted while waiting for the exchange,
then InterruptedException is thrown and the current thread's interrupted status is cleared.

如果经过了指定的等待时间,则抛出TimeoutException 如果时间小于或等于零,该方法将不会等待。

Parameters
x V: the object to exchange
timeout long: the maximum time to wait
unit TimeUnit: the time unit of the timeout argument
Returns
V the object provided by the other thread
Throws
InterruptedException if the current thread was interrupted while waiting
TimeoutException if the specified waiting time elapses before another thread enters the exchange

exchange

Added in API level 1
V exchange (V x)

等待另一个线程到达此交换点(除非当前线程为 interrupted ),然后将给定对象传送给它,从而接收其对象。

如果另一个线程已经在交换点等待,那么为了线程调度目的它将被恢复,并接收当前线程传入的对象。 当前线程立即返回,接收由另一个线程传递给交换机的对象。

如果没有其他线程已经在交换机上等待,那么为了线程调度目的,当前线程被禁用,并且处于休眠状态,直到发生以下两件事之一:

  • Some other thread enters the exchange; or
  • Some other thread interrupts the current thread.

如果当前线程:

  • has its interrupted status set on entry to this method; or
  • is interrupted while waiting for the exchange,
then InterruptedException is thrown and the current thread's interrupted status is cleared.

Parameters
x V: the object to exchange
Returns
V the object provided by the other thread
Throws
InterruptedException if the current thread was interrupted while waiting

Hooray!