Exchange source code analysis – java8
•
Java
1. Characteristic analysis
package sourcecode. analysis;
import java.util.concurrent.Exchanger; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
public class Main { private static final Exchanger<String> exch=new Exchanger<>();//定义交换器 private static final ExecutorService threadPool= Executors.newFixedThreadPool(2);//定义线程池中服务线程个数 public static void main(String[] args) { //定义银行流水A Runnable r1=()->{ String stra="银行流水A"; try{ exch.exchange(stra); }catch (InterruptedException e){ e.printStackTrace(); } }; //定义银行流水B Runnable r2=()->{ String strb="银行流水B"; try{ String recesive= exch.exchange(strb); //判定B的原信息和<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>信息是否相等 Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("B的原item和从A<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>的item是否相等:"+strb.equals(recesive)); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("原item为:"+strb); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("从A<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>的item为:"+recesive); }catch (InterruptedException e){ e.printStackTrace(); } }; threadPool.execute(r1); threadPool.execute(r2); threadPool.shutdown(); } }
Output results:
B的原item和从A获取的item是否相等:false 原item为:银行流水B 从A获取的item为:银行流水A Process finished with exit code 0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码