Java基础-Exchanger的使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的使用的主要内容,如果未能解决你的问题,请参考以下文章