线程之间如何交换数据?

JUC中的Exchanger

java.util.concurrent.Exchanger实现了两个线程之间交换数据

交换器类Exchanger
在这里插入图片描述

方法说明
V exchange(V x)交换数据,没有线程交换就一直阻塞
V exchange(V x, long timeout, TimeUnit unit)在指定时间内没有线程交换,就抛出超时异常

简单测试exchange(V x)

public class Test1 {
	//创建交换器类的实例
    static Exchanger exchanger = new Exchanger();

    public static void main(String[] args) {
        new Thread(() -> {
            Object data = "AAA";
            System.out.println(Thread.currentThread().getName() + ":" + data);

            //交换数据
            try {
                data = exchanger.exchange(data);
                System.out.println(Thread.currentThread().getName() + ":" + data);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        new Thread(() -> {
            Object data = "BBB";
            System.out.println(Thread.currentThread().getName() + ":" + data);

            //交换数据
            try {
                data = exchanger.exchange(data);
                System.out.println(Thread.currentThread().getName() + ":" + data);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
}

在这里插入图片描述

超时测试 exchange(V x, long timeout, TimeUnit unit)

public class Test2 {
    static Exchanger exchanger = new Exchanger();

    public static void main(String[] args) {
        new Thread(() -> {
            Object data = "AAA";
            System.out.println(Thread.currentThread().getName() + ":" + data);

            //交换数据
            try {
            	//                   交换的数据,超时时间,时间单位
                data = exchanger.exchange(data,3000, TimeUnit.MICROSECONDS);
                System.out.println(Thread.currentThread().getName() + ":" + data);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (TimeoutException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
}

没有其他线程与之进行数据交换,超时后抛出异常
在这里插入图片描述

中断测试

一个线程开始交换数据后,如果没有其他线程和它交换,就会一直阻塞,直到有线程交换、中断、超时等情况出现。

阻塞…

中断
开启线程数据交换后,中断线程

public class Test3 {
    static Exchanger exchanger = new Exchanger();

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            Object data = "AAA";
            System.out.println(Thread.currentThread().getName() + ":" + data);

            //交换数据
            try {
                data = exchanger.exchange(data);
                System.out.println(Thread.currentThread().getName() + ":" + data);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        t.start();
        Thread.sleep(3000);
        t.interrupt();//线程中断
    }
}

在这里插入图片描述
中断线程后,线程停止阻塞,结束运行

理解Exchanger两两交换数据

十个线程之间是如何交换数据的?

public class Test4 {
    static Exchanger exchanger = new Exchanger();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Integer data = i;
            new Thread(() -> {
                //交换数据
                try {
                    Object res = exchanger.exchange(data);
                    System.out.println(Thread.currentThread().getName() + ":" + res);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }
    }
}

在这里插入图片描述
可以看到10个线程两两之间交换了数据

如果9个线程之间交换数据,会发生什么捏?
在这里插入图片描述
9个线程之间交换数据,有四对线程互换数据了,最后一个线程不参与交换

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值