concurrent包下的Exchanger练习

Exchanger可以在两个线程之间交换数据,只能是2个线程,他不支持更多的线程之间互换数据。

当线程A调用Exchange对象的exchange()方法后,他会陷入阻塞状态,直到线程B也调用了exchange()方法,然后以线程安全的方式交换数据,之后线程A和B继续运行。

 1 package cn.sp.test4;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.concurrent.Exchanger;
 6 
 7 /**
 8  * Created by 2YSP on 2017/9/1.
 9  * 两个线程之间交换数据
10  */
11 public class ExchangerTest {
12 
13 
14     public static void main(String[] args) {
15         final Exchanger<List<Integer>> exchanger = new Exchanger<>();
16 
17         new Thread(() -> {
18             List<Integer> l = new ArrayList<Integer>(2);
19             l.add(1);
20             l.add(2);
21             try {
22                 l = exchanger.exchange(l);
23             } catch (InterruptedException e) {
24                 e.printStackTrace();
25             }
26             System.out.println("thread1: " + l);
27         }).start();
28 
29         new Thread(() -> {
30             List<Integer> l = new ArrayList<Integer>(2);
31             l.add(3);
32             l.add(4);
33             try {
34                 l = exchanger.exchange(l);
35             } catch (InterruptedException e) {
36                 e.printStackTrace();
37             }
38             System.out.println("thread2: " + l);
39         }).start();
40 
41     }
42 }

执行结果:

可以看到线程1和2已经交换了数据。

转载于:https://www.cnblogs.com/2YSP/p/7483584.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python默认参数

Python | 默认参数 (Python | default parameters) A default parameter is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesnt provide a value for the parameter with the default value. …

最长公共前缀_最长的公共前缀

最长公共前缀Problem statement: 问题陈述&#xff1a; Write a function to find the longest common prefix string amongst an array of strings. 编写函数以在字符串数组中找到最长的公共前缀字符串 。 If there is no common prefix, return an empty string "&quo…

物联网听起来像是一个和互联网不同的网,万物互联又把网给弄丢了,正向我们扑面而来的是万物互联网。...

物联网听起来像是一个和互联网不同的网&#xff0c;"万物互联"又把"网"给弄丢了&#xff0c;正向我们扑面而来的是"万物互联网"。转载于:https://www.cnblogs.com/beingonline/p/7484135.html

sdram trp_TRP的完整形式是什么?

sdram trpTRP&#xff1a;电视收视点 (TRP: Television Rating Point) TRP is an abbreviation of "Television Rating Point". TRP是“电视评分点”的缩写 。 It is a system or standard of measurement which signifies the demand and popularity of a televisi…

Controller计算值传到jsp页面,用session传值

HttpSession session request.getSession(); session.setAttribute("key",value); jap 用 ${key}来接收该值 转载于:https://www.cnblogs.com/douder/p/7484491.html

CBT的完整形式是什么?

CBT&#xff1a;基于计算机的培训 (CBT: Computer Based Training) CBT is an abbreviation of "Computer-based training". CBT是“基于计算机的培训”的缩写 。 It is a training program which entails the use of a personal system or networked computer. The…

论道社会化商业

主持人 用友优普副总裁傅毅&#xff1a; 谢谢各位嘉宾&#xff0c;我们会留一些时间让在座的嘉宾提问。请各位嘉宾用一个非常简单的一句话&#xff0c;或者几个关键词&#xff0c;总结一下你认为的社会化商业是什么&#xff1f; 用友优普执行总裁 徐洋&#xff1a; 社会化商业为…

CChelper彩虹SDK可视远程客服解决方案

本文讲的是 : CChelper彩虹SDK可视远程客服解决方案 , 在智能生态产业链中&#xff0c;智能硬件终端是把握消费者的直接环节&#xff0c;随着物联网时代迈向成熟&#xff0c;智能家居领域的硬件逐渐成为智能硬件终端的主角。目前的市场环境下&#xff0c;智能家居领域的自身硬…

matlab 简介_MATLAB简介

matlab 简介MATLAB简介 (MATLAB Introduction) MATLAB was designed by Cleve Moler for his student in 1970s but after some time jack little, an engineer realized its potential and rewrote it at the MathWorks, and it was rewritten in C language by the date of 1…

Scala中的嵌套循环

Scala中的嵌套循环 (Nested loop in Scala) In programming, a nested loop is used in initializing or iterate multi-dimensional array or to print patterns. Scala provides an efficient method to use nested loops in the programming language. The most used nested…

python基础-字典

字典 # 字典是python基本数据结构之一&#xff0c;相对于列表和元组&#xff0c;他是无序的&#xff0c;每次输出都打乱了顺序&#xff0c;没有下标hello{110:{"name":"alex","age":28,"home":"shandong"},111:{"name&…

sql算术运算符_SQL中的算术运算符

sql算术运算符SQL | 算术运算符 (SQL | Arithmetic Operators) Different number-crunching administrators are utilized in SQL to be specific Addition (), Subtraction (-), Multiplication (*), Division (/), Modulus (%). SQL中使用了不同的数字运算管理员来表示特定的…

HDU 6188 Duizi and Shunzi

栈。 将数字排序后&#xff0c;一个一个压入栈。如果栈顶两个元素形成了对子&#xff0c;那么$ans1$&#xff0c;弹出栈顶两个元素&#xff1b;如果栈顶三个元素形成了顺子&#xff0c;那么$ans1$&#xff0c;弹出栈顶三个元素。 #include<bits/stdc.h> using namespace …

php 单例模式有什么缺点_PHP的完整形式是什么?

php 单例模式有什么缺点PHP&#xff1a;超文本预处理器 (PHP: Hypertext Preprocessor ) PHP is an abbreviation of Hypertext Preprocessor, earlier called Personal Home Page. PHP is extensively used HTML-embedded, open-source server-side scripting language create…

Myeclipse有关的问题

Myeclipse配置问题 1.行数显示 window ----preference----General-----Editors-----TextEditors----show line numbers 2.编码设置 window ---preference----workspace-----设置 3.jsp编码设置 window ---preference----myeclipse------Files And Editors------jsp 4.jsp的视图…

weak-to-strong-generalization始终比母体更智能的人工智能,能否被它的母体所监管supervision,从而变的更强

正如supervison这个词&#xff0c;就像就是母亲对孩子的超级super愿景vision&#xff0c;比母亲更聪明更强&#xff0c;也就意味着要按照母亲期望的那样成长&#xff0c;不合理的行为要能够纠正supervison。 一代比一代强&#xff0c;一代比一代好。 弱模型监督能否激发出更强…

最小跳数

Description: 描述&#xff1a; This problem is a standard interview problem which has been featured in interview rounds of Adobe, Amazon, Oyo rooms etc. 此问题是标准的采访问题&#xff0c;已在Adobe&#xff0c;Amazon&#xff0c;Oyo房间等的采访回合中出现。 P…

《Web安全之机器学习入门》一 第3章 机器学习概述

第3章 机器学习概述机器学习的概念非常多&#xff0c;从有监督到无监督&#xff0c;从聚类到回归&#xff0c;从浅层学习到深度学习&#xff0c;从准确率到召回率&#xff0c;它们究竟是什么意思呢&#xff1f;本章将介绍最主要的几个概念。不少机器学习初学者甚至包括业内老司…

ue 抗锯齿 渲染序列失灵_最大的锯齿形序列

ue 抗锯齿 渲染序列失灵Problem statement: 问题陈述&#xff1a; Given a square matrix of size n x n, find the sum of the Zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence…

团队-团队编程项目作业名称-成员简介及分工

成员&#xff1a;祁昊 分工&#xff1a;ui设计&#xff0c;美工&#xff0c;详细设计。转载于:https://www.cnblogs.com/qihao10086/p/7496101.html