Java并发编程之Exchanger方法详解
作者:我真的不会Java啊 时间:2022-08-22 02:44:41
简介
Exchanger是一个用于线程间数据交换的工具类,它提供一个公共点,在这个公共点,两个线程可以交换彼此的数据。
当一个线程调用exchange方法后将进入等待状态,直到另外一个线程调用exchange方法,双方完成数据交换后继续执行。
Exchanger的使用
方法介绍
exchange(V x):阻塞当前线程,直到另外一个线程调用exchange方法或者当前线程被中断。
x : 需要交换的对象。
exchange(V x, long timeout, TimeUnit unit):阻塞当前线程,直到另外一个线程调用exchange方法或者当前线程被中断或者等待超时。
x: 需要交换的对象。
timeout:超时时间。
unit:超时时间单位。
exchange方法正常情况返回交换到的对象,当当前线程被中断或者等待超时时,exchange方法返回null。
示例1:A同学和B同学交换各自收藏的大片
public class Demo {
public static void main(String[] args) {
Exchanger<String> stringExchanger = new Exchanger<>();
Thread studentA = new Thread(() -> {
try {
String dataA = "A同学收藏多年的大片";
String dataB = stringExchanger.exchange(dataA);
System.out.println("A同学得到了" + dataB);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread studentB = new Thread(() -> {
try {
String dataB = "B同学收藏多年的大片";
String dataA = stringExchanger.exchange(dataB);
System.out.println("B同学得到了" + dataA);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
studentA.start();
studentB.start();
}
}
/*
* 输出结果:
* B同学得到了A同学收藏多年的大片
* A同学得到了B同学收藏多年的大片
*/
示例2:A同学被放鸽子,交易失败
public class Demo {
public static void main(String[] args) {
Exchanger<String> stringExchanger = new Exchanger<>();
Thread studentA = new Thread(() -> {
String dataB = null;
try {
String dataA = "A同学收藏多年的大片";
//最多等待5秒
dataB = stringExchanger.exchange(dataA, 5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException ex){
System.out.println("等待超时-TimeoutException");
}
System.out.println("A同学得到了" + dataB);
});
studentA.start();
}
}
/*
* 输出结果:
* 等待超时-TimeoutException
* A同学得到了null
*/
来源:https://www.cnblogs.com/seve/archive/2021/04/11/14640642.html
标签:Java,并发编程,Exchanger
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
阿里nacos+springboot+dubbo2.7.3统一处理异常的两种方式
2022-12-05 03:50:25
![](https://img.aspxhome.com/file/2023/4/70944_0s.png)
C#中的TemplateMethod模式问题分析
2021-12-01 04:02:44
![](https://img.aspxhome.com/file/2023/3/79103_0s.png)
SpringBoot访问windows共享文件的方法
2023-11-08 13:37:13
Java使用Redis实现秒杀功能
2023-04-11 11:26:54
![](https://img.aspxhome.com/file/2023/1/78001_0s.jpg)
Java使用DOM4j实现读写XML文件的属性和元素
2021-10-19 00:21:52
详解Java反射创建对象
2022-12-10 03:52:23
![](https://img.aspxhome.com/file/2023/4/78024_0s.png)
java jdbc连接和使用详细介绍
2023-01-13 18:12:52
Flutter Widgets之标签类控件Chip详解
2023-06-26 14:22:35
![](https://img.aspxhome.com/file/2023/0/120090_0s.jpg)
java金额数字转中文工具类详解
2022-02-15 09:42:25
Spring之spring-context-indexer依赖详解
2023-11-23 12:21:41
![](https://img.aspxhome.com/file/2023/4/59024_0s.jpg)
Java数据结构学习之二叉树
2023-04-25 12:08:27
![](https://img.aspxhome.com/file/2023/1/68171_0s.png)
formfile文件上传使用示例
2023-06-23 03:41:58
java实现通讯录管理系统
2021-07-02 00:19:53
![](https://img.aspxhome.com/file/2023/2/71232_0s.jpg)
一文了解Spring中拦截器的原理与使用
2023-06-30 13:28:05
![](https://img.aspxhome.com/file/2023/5/65735_0s.png)
浅析java中的取整(/)和求余(%)
2023-04-30 23:46:23
Java monitor机制使用方法解析
2023-11-09 11:25:56
![](https://img.aspxhome.com/file/2023/3/58563_0s.png)
C# Socket编程实现简单的局域网聊天器的示例代码
2022-10-01 19:45:45
![](https://img.aspxhome.com/file/2023/6/85716_0s.png)
java贪吃蛇游戏编写代码
2023-06-16 02:41:10
![](https://img.aspxhome.com/file/2023/3/74753_0s.jpg)
详解Spring Boot最核心的27个注解,你了解多少?
2023-11-20 06:23:35
spring aop之链式调用的实现
2022-10-30 05:56:50
![](https://img.aspxhome.com/file/2023/4/84654_0s.png)