栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

线程之间如何交换数据?

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程之间如何交换数据?

线程之间如何交换数据?
  • JUC中的Exchanger
    • 简单测试exchange(V x)
    • 超时测试 exchange(V x, long timeout, TimeUnit unit)
    • 中断测试
    • 理解Exchanger两两交换数据

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个线程之间交换数据,有四对线程互换数据了,最后一个线程不参与交换

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016852.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号