【详解】JUC之Exchanger

简介

  • 用于两个工作线程之间交换数据的封装工具类
  • 简单说就是一个线程在完成一定的事务后想与另一个线程交换数据,则第一个先拿出数据的线程会一直等待第二个线程,直到第二个线程拿着数据到来时才能彼此交换对应数据

Exchanger<V> 泛型类型,其中 V 表示可交换的数据类型

简单的应用

public class ExchangerTest {

    public static void main(String[] args) {
        final Exchanger<String> exchanger = new Exchanger<>();


        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + " start . ");
            try {
                String exchange = exchanger.exchange("I am come from T-A");
                System.out.println(Thread.currentThread().getName() + " get value : " + exchange);
                System.out.println(Thread.currentThread().getName() + " end . ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"A").start();


        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + " start . ");
            try {
                String exchange = exchanger.exchange("I am come from T-B");
                System.out.println(Thread.currentThread().getName() + " get value : " + exchange);
                System.out.println(Thread.currentThread().getName() + " end . ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"B").start();
    }
}

结果:

A start . 
B start . 
B get value : I am come from T-A
A get value : I am come from T-B
A end . 
B end . 

可以看出最重要的方法是exchange

重要方法介绍

public V exchange(V x) throws InterruptedException
  • 等待另一个线程到达此交换点,然后将给定对象传输给它,接收其对象作为回报。
  • 可以被打断
  • 如果已经有个线程正在等待了,则直接交换数据

发送的数据的分析

public class ExchangerTest {

    public static void main(String[] args) {

        Object o = new Object();

        final Exchanger<Object> exchanger = new Exchanger<>();


        new Thread(()->{
            try {
                Object exchange = exchanger.exchange(null);
                System.out.println("接收的" + o );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"A").start();

        new Thread(()->{
            try {
                System.out.println("发送的" + o);
                exchanger.exchange(o);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"B").start();
    }
}

结果

发送的java.lang.Object@67deb479
接收的java.lang.Object@67deb479

从这个例子可以看出一个很严重的问题发送的对象和接收的对象是同一个对象,可能会用严重的线程安全问题

猜你喜欢

转载自blog.csdn.net/qq_43040688/article/details/105955788