package com.test;import java.util.Random;import java.util.concurrent.Exchanger;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ExchangerTest{ /** * @param args */ public static void main(String[] args) { ExecutorService pool = Executors.newCachedThreadPool(); final Exchanger changer = new Exchanger(); pool.execute(new Runnable() { public void run() { String data1 = "情报1"; System.out.println("线程:" + Thread.currentThread().getName() + "准备把:" + data1+"交换出去"); try { Thread.sleep(new Random().nextInt(10000)); String data2 = (String)changer.exchange(data1); System.out.println("线程:" + Thread.currentThread().getName() + "获得了:" + data2); } catch (Exception e) { e.printStackTrace(); } } }); pool.execute(new Runnable() { public void run() { String data1 = "情报2"; System.out.println("线程:" + Thread.currentThread().getName() + "准备把:" + data1+"交换出去"); try { Thread.sleep(new Random().nextInt(10000)); String data2 = (String)changer.exchange(data1); System.out.println("线程:" + Thread.currentThread().getName() + "获得了:" + data2); } catch (Exception e) { e.printStackTrace(); } } }); pool.shutdown(); }}
详细解决方案
Exchanger关于互换情报的例子
热度:7200 发布时间:2013-02-26 00:00:00.0
相关解决方案
- java.util.concurrent详解(二)Se地图hore/FutureTask/Exchanger
- 同步器-Exchanger
- JAVA5线程并发库[八]-Exchanger(转)
- java并发 Exchanger (6)
- Exchanger-弟兄线程的信息交换
- java 并发之(2)同步器(synchronizer)FutureTask 、Exchanger
五、Exchanger
- 线程十七:Exchanger
- 并发包 Semaphore,CountDownLatch,CyclicBarrier,Exchanger 实例:
- Java 并发 (12) -- Exchanger 类
- Java Exchanger
- JUC---Exchanger
- JUC学习系列九(同步点 Exchanger)
- java并发编程汇总12:并发工具类:Semaphore,Exchanger,CountDownLatch,CyclicBarrier
- 详解 Java并发工具类:Semaphore,Exchanger
- Exchanger 介绍及应用详解
- JAVA多线程与高并发(三)[ReentrantLock,ReadWriteLock,CountdownLatch,CyclicBarrier,Phaser,Semaphore,Exchanger]
- 07Java多线程-31. 并发工具类(CountDownLatch,Semaphore,CyclicBarrier,Exchanger)的使用
- 并发编程:Exchanger 两个并发任务间的数据交换