Exchanger学习一

一、定义
Exchanger是一个用于线程间数据交换的工具类,它提供一个公共点,在这个公共点,两个线程可以交换彼此的数据。
当一个线程调用exchange方法后将进入等待状态,直到另外一个线程调用exchange方法,双方完成数据交换后继续执行。
Exchanger 是 JDK 1.5 开始提供的一个用于两个工作线程之间交换数据的封装工具类,简单说就是一个线程在完成一定的事务后想与另一个线程交换数据,则第一个先拿出数据的线程会一直等待第二个线程,直到第二个线程拿着数据到来时才能彼此交换对应数据。
从定义来看Exchanger是用于线程间进行通信、数据交换。Exchanger提供了一个同步点exchange方法,两个线程调用exchange方法时,无论调用时间先后,两个线程会互相等到线程到达exchange方法调用点,此时两个线程可以交换数据,将本线程产出数据传递给对方。

    /*** Waits for another thread to arrive at this exchange point (unless* the current thread is {@linkplain Thread#interrupt interrupted}),* and then transfers the given object to it, receiving its object* in return.** <p>If another thread is already waiting at the exchange point then* it is resumed for thread scheduling purposes and receives the object* passed in by the current thread.  The current thread returns immediately,* receiving the object passed to the exchange by that other thread.** <p>If no other thread is already waiting at the exchange then the* current thread is disabled for thread scheduling purposes and lies* dormant until one of two things happens:* <ul>* <li>Some other thread enters the exchange; or* <li>Some other thread {@linkplain Thread#interrupt interrupts}* the current thread.* </ul>* <p>If the current thread:* <ul>* <li>has its interrupted status set on entry to this method; or* <li>is {@linkplain Thread#interrupt interrupted} while waiting* for the exchange,* </ul>* then {@link InterruptedException} is thrown and the current thread's* interrupted status is cleared.** @param x the object to exchange* @return the object provided by the other thread* @throws InterruptedException if the current thread was*         interrupted while waiting*/@SuppressWarnings("unchecked")public V exchange(V x) throws InterruptedException {Object v;Object item = (x == null) ? NULL_ITEM : x; // translate null argsif ((arena != null ||(v = slotExchange(item, false, 0L)) == null) &&((Thread.interrupted() || // disambiguates null return(v = arenaExchange(item, false, 0L)) == null)))throw new InterruptedException();return (v == NULL_ITEM) ? null : (V)v;}
    /*** Waits for another thread to arrive at this exchange point (unless* the current thread is {@linkplain Thread#interrupt interrupted} or* the specified waiting time elapses), and then transfers the given* object to it, receiving its object in return.** <p>If another thread is already waiting at the exchange point then* it is resumed for thread scheduling purposes and receives the object* passed in by the current thread.  The current thread returns immediately,* receiving the object passed to the exchange by that other thread.** <p>If no other thread is already waiting at the exchange then the* current thread is disabled for thread scheduling purposes and lies* dormant until one of three things happens:* <ul>* <li>Some other thread enters the exchange; or* <li>Some other thread {@linkplain Thread#interrupt interrupts}* the current thread; or* <li>The specified waiting time elapses.* </ul>* <p>If the current thread:* <ul>* <li>has its interrupted status set on entry to this method; or* <li>is {@linkplain Thread#interrupt interrupted} while waiting* for the exchange,* </ul>* then {@link InterruptedException} is thrown and the current thread's* interrupted status is cleared.** <p>If the specified waiting time elapses then {@link* TimeoutException} is thrown.  If the time is less than or equal* to zero, the method will not wait at all.** @param x the object to exchange* @param timeout the maximum time to wait* @param unit the time unit of the {@code timeout} argument* @return the object provided by the other thread* @throws InterruptedException if the current thread was*         interrupted while waiting* @throws TimeoutException if the specified waiting time elapses*         before another thread enters the exchange*/@SuppressWarnings("unchecked")public V exchange(V x, long timeout, TimeUnit unit)throws InterruptedException, TimeoutException {Object v;Object item = (x == null) ? NULL_ITEM : x;long ns = unit.toNanos(timeout);if ((arena != null ||(v = slotExchange(item, true, ns)) == null) &&((Thread.interrupted() ||(v = arenaExchange(item, true, ns)) == null)))throw new InterruptedException();if (v == TIMED_OUT)throw new TimeoutException();return (v == NULL_ITEM) ? null : (V)v;}
V exchange(V v):等待另一个线程到达此交换点(除非当前线程被中断),然后将给定的对象传送给该线程,并接收该线程的对象。
V exchange(V v, long timeout, TimeUnit unit):等待另一个线程到达此交换点,或者当前线程被中断——抛出中断异常;又或者是等候超时——抛出超时异常,然后将给定的对象传送给该线程,并接收该线程的对象。

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

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

相关文章

法线贴图实现衣服上皱褶特效

在线工具推荐&#xff1a; 3D数字孪生场景编辑器 - GLTF/GLB材质纹理编辑器 - 3D模型在线转换 - Three.js AI自动纹理开发包 - YOLO 虚幻合成数据生成器 - 三维模型预览图生成器 - 3D模型语义搜索引擎 法线贴图在3D建模中扮演着重要的角色&#xff0c;它通过模拟表面的微…

c++内存池项目

文章目录 一、内存池介绍二、ThreadCache实现三、CentralCache实现四、PageCache实现五、回收内存六、大于256KB的内存申请与释放七、将new和delete换为定长内存池八、多线程环境下对比malloc进行基准测试九、使用基数树进行性能优化 一、内存池介绍 二、ThreadCache实现 下面…

springboot集成springdoc-openapi(模拟前端请求)

目录 描述---痛点 Springfox对比springdoc-openapi 1. 成熟度和维护性&#xff1a; 2. 依赖和配置&#xff1a; 3. 注解和使用方式&#xff1a; 4. 特性和扩展性&#xff1a; 应用目录结构 pom文件 新增测试controller StaffController YUserController 启动测试看下…

Java基础回顾——注解

文章目录 介绍定义注解处理注解使用注解 介绍 注解Annotation&#xff1a;是放在Java源码的类、方法、字段、参数前的一种特殊注释 注释会被编译器直接忽略&#xff0c;注解则可以被编译器打包进入class文件&#xff0c;因此&#xff0c;注解是一种用作标注的“元数据”。 作…

PHP HTTPoxy CGI 应用程序漏洞 CVE-2016-5385

HTTPoxy CGI 应用程序漏洞 CVE-2016-5385 已亲自复现 漏洞名称漏洞描述影响版本 漏洞复现环境搭建漏洞利用 修复建议 漏洞名称 漏洞描述 在Oracle Communications BRM 10.x/12.x&#xff08;云软件&#xff09;中发现漏洞。它已经被宣布为关键。此漏洞影响组件用户数据库的未…

Linux笔记---用户和权限管理基本命令介绍

&#x1f34e;个人博客&#xff1a;个人主页 &#x1f3c6;个人专栏&#xff1a;Linux学习 ⛳️ 功不唐捐&#xff0c;玉汝于成 目录 ​编辑 前言&#xff1a; 命令&#xff1a; whoami&#xff1a; passwd&#xff1a; useradd&#xff1a; userdel&#xff1a; chm…

华为交换配置OSPF与BFD联动

实验拓扑 组网需求 如图所示&#xff0c;SW1、SW2和SW3之间运行OSPF&#xff0c;SW1和SW2之间的交换机仅作透传功能。现在需要SW1和SW2能够快速感应它们之间的链路状态&#xff0c;当链路SW1-SW2发生故障时&#xff0c;业务能快速切换到备份链路SW1-SW3-SW2上 配置思路 采用…

文件的基本管理

目录 一、Linux系统目录结构和相对/绝对路径 &#xff08;一&#xff09;系统目录结构 &#xff08;二&#xff09;相对路径和绝对路径 1.绝对路径 2.相对路径 &#xff08;三&#xff09;通配符的作用 二、创建、复制、删除文件&#xff0c;rm -rf /意外事故 &#xf…

说说对React Hooks的理解?解决了什么问题?

面试官&#xff1a;说说对React Hooks的理解&#xff1f;解决了什么问题&#xff1f; 一、是什么 Hook 是 React 16.8 的新增特性。它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性 至于为什么引入hook&#xff0c;官方给出的动机是解决长时间使用和维护…

山景DU561—32位高性能音频处理器(DSP)芯片

音频处理可以更好地捕捉和处理声音和音乐&#xff1b;而DSP音频处理芯片是一种利用数字信号处理技术进行音频处理的专用芯片&#xff1b;可用于多种应用&#xff0c;从音乐拾音到复杂的音频信号处理&#xff0c;和声音增强。 由工采网代理的山景DU561是一款集成多种音效算法高…

C# 获取本机IP地址的方法

在C#环境中&#xff0c;要获取本机的IP地址&#xff0c;可以使用以下方法&#xff1a; 1、使用NetworkInterface类和IPAddress类&#xff1a; using System; using System.Net; using System.Net.NetworkInformation;class Program {static void Main(){// 获取本地计算机上的…

08、基于LunarLander登陆器的DDQN强化学习(含PYTHON工程)

08、基于LunarLander登陆器的DDQN强化学习&#xff08;含PYTHON工程&#xff09; LunarLander复现&#xff1a; 07、基于LunarLander登陆器的DQN强化学习案例&#xff08;含PYTHON工程&#xff09; 08、基于LunarLander登陆器的DDQN强化学习&#xff08;含PYTHON工程&#xf…

登录注册表单路由切换 - 登录注册开发入门(6)

登录注册表单路由切换 - 登录注册开发入门(6) 教程目标 完成注册表单的搭建&#xff0c;并实现与登录表单的无缝切换。 教程步骤 创建路由容器 在页面中添加一个路由容器。路由容器需要与路由的页面容器配合使用。 添加页面组件 在路由容器下方添加两个页面组件&#xff0c;…

普通虚拟主机如何安装SSL证书?

在今天的互联网世界中&#xff0c;保护网站数据安全和用户隐私已经成为了每个网站拥有者的重要任务。尽管安装SSL证书在普通虚拟主机环境中可能会有一些挑战&#xff0c;但通过正确的步骤和配置&#xff0c;您仍然可以为您的网站提供更高级别的安全保护。普通虚拟主机如何安装S…

DTC营销新模式,创新商业引领裂变营销新潮流的玩法!

DTC营销新模式&#xff0c;创新商业引领裂变营销新潮流的玩法&#xff01; 随着市场竞争的加剧&#xff0c;企业寻求创新的营销模式以突破困境&#xff0c;脱颖而出。其中&#xff0c;DTC&#xff08;Direct-to-Consumer&#xff0c;直接面向消费者&#xff09;营销新模式应运…

【mysql】MySql中死锁是什么?怎么解决?

在MySQL中,死锁是指两个或多个事务相互等待对方持有的资源,导致它们无法继续执行并永远地被阻塞的情况。每个事务都持有一些资源,并且等待其他事务释放资源,但由于循环依赖关系,导致所有事务都无法继续执行,从而形成死锁。 当发生死锁时,MySQL会自动检测到它,并选择其…

CentOS 7 制作openssh 9.6 rpm包更新修复安全漏洞 —— 筑梦之路

2023年12月18日 openssh 发布新版9.6p1&#xff0c;详细内容阅读OpenSSH: Release Notes 背景说明 之前也写过多篇制作openssh rpm包的文章&#xff0c;为何要重新来写一篇制作openssh 9.6版本的&#xff1f; openssh 9.6 rpm包制作和之前存在区别&#xff0c;对于CentOS 7来…

spring之面向切面:AOP(2)

学习的最大理由是想摆脱平庸&#xff0c;早一天就多一份人生的精彩&#xff1b;迟一天就多一天平庸的困扰。各位小伙伴&#xff0c;如果您&#xff1a; 想系统/深入学习某技术知识点… 一个人摸索学习很难坚持&#xff0c;想组团高效学习… 想写博客但无从下手&#xff0c;急需…

【Python】函数

一、函数介绍 二、函数的定义 三、函数的参数 四、函数的返回值 五、函数说明文档 六、函数的嵌套调用 七、变量的作用域 一、函数介绍 函数的使用 函数的作用 函数 函数&#xff1a;是组织好的&#xff0c;可重复使用的&#xff0c;用来实现特定功能的代码段。 input()、p…

文件批量管理,按单值大小归类保存,提升工作效率与便捷性!

你是否曾经遇到过需要批量管理大量文件&#xff0c;但却因为文件大小不一而感到混乱&#xff1f;你是否希望有一种方法能够将这些文件按照单值大小进行归类保存&#xff0c;以便更方便地管理和查找&#xff1f;现在&#xff0c;我们有一个好消息要告诉你&#xff0c;我们推出了…