📌  相关文章
📜  带有示例的Java.util.concurrent.Exchanger 类

📅  最后修改于: 2022-05-13 01:55:49.789000             🧑  作者: Mango

带有示例的Java.util.concurrent.Exchanger 类

Exchanger是Java中最有趣的同步类。它通过创建同步点来促进一对线程之间的元素交换。它简化了两个线程之间的数据交换。它的操作很简单:它只是等待两个单独的线程调用它的exchange()方法。发生这种情况时,它会交换线程提供的数据。它也可以看作是一个双向的 SynchronousQueue 。它是一个通用类,声明如下。

类语法:

Exchanger

这里,V 指定了正在交换的数据的类型。

类层次结构

java.lang.Object
↳ java.util.concurrent.Exchanger

构造函数:

  1. Exchanger() – 为其成员创建一个具有默认值的新 Exchanger 对象。

方法:

  1. exchange(V x) – 调用此函数时,会导致当前线程暂停其执行并等待另一个线程调用其交换方法。当另一个线程调用它的交换方法时,线程交换它们的数据并恢复执行。

    句法:

    public V exchange(V x)
    throws InterruptedException
    
  2. exchange(V x, long timeout, TimeUnit unit) – 调用此函数时,会导致当前线程暂停其执行并等待另一个线程调用其交换方法。当另一个线程调用它的交换方法时,线程交换它们的数据并恢复执行。线程仅等待 timeout 参数指定的持续时间,如果超时持续时间已过,则会引发 TimeoutException。

    句法:

    public V exchange(V x, long timeout, TimeUnit unit)
    throws InterruptedException, TimeoutException
    

演示 Exchanger 类工作的示例:

import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
  
public class ExchangerDemo {
    public static void main(String[] args)
    {
        Exchanger exchanger = new Exchanger<>();
  
        new UseString(exchanger);
        new MakeString(exchanger);
    }
}
  
// A thread that makes a string
class MakeString implements Runnable {
    Exchanger ex;
    String str;
  
    MakeString(Exchanger ex)
    {
        this.ex = ex;
        str = new String();
  
        new Thread(this).start();
    }
  
    public void run()
    {
        char ch = 'A';
        try {
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 5; j++) {
                    str += ch++;
                }
                if (i == 2) {
                    // Exchange the buffer and
                    // only wait for 250 milliseconds
                    str
                        = ex.exchange(str,
                                      250,
                                      TimeUnit.MILLISECONDS);
                    continue;
                }
  
                // Exchange a full buffer for an empty one
                str = ex.exchange(str);
            }
        }
        catch (InterruptedException e) {
            System.out.println(e);
        }
        catch (TimeoutException t) {
            System.out.println("Timeout Occurred");
        }
    }
}
  
// A thread that uses a string
class UseString implements Runnable {
  
    Exchanger ex;
    String str;
  
    UseString(Exchanger ex)
    {
        this.ex = ex;
  
        new Thread(this).start();
    }
  
    public void run()
    {
        try {
            for (int i = 0; i < 3; i++) {
                if (i == 2) {
                    // Thread sleeps for 500 milliseconds
                    // causing timeout
                    Thread.sleep(500);
                    continue;
                }
  
                // Exchange an empty buffer for a full one
                str = ex.exchange(new String());
                System.out.println("Got: " + str);
            }
        }
        catch (InterruptedException e) {
            System.out.println(e);
        }
    }
}
输出:
Got: ABCDE
Got: FGHIJ
Timeout Occurred

参考: https: Java/util/concurrent/Exchanger.html