首页 > 代码库 > Java基础-Exchanger的使用
Java基础-Exchanger的使用
java基础Exchanger的使用
package com.java.thread.test; import java.util.concurrent.Exchanger; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ExchangeerTest { private static final Exchanger<String> exgr=new Exchanger<>(); private static ExecutorService threadPool = Executors.newFixedThreadPool(2); public static void main(String[] args) { Future<?> r1 = threadPool.submit(new Runnable() { @Override public void run() { String A = "银行流水A"; try { String B=exgr.exchange(A); System.out.println("B:"+B); } catch (InterruptedException e) { e.printStackTrace(); } } }); threadPool.submit(new Runnable() { @Override public void run() { String B = "银行流水B"; String A = null; try { A = exgr.exchange(B); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("A和B是否一致:" + A.equals(B) + ",A录入的是:" + A + ",B录入的是:" + B); } }); threadPool.shutdown(); } }
Java基础-Exchanger的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。