【详解】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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值