Exchange的介绍及使用
生活随笔
收集整理的這篇文章主要介紹了
Exchange的介绍及使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作用
用于 兩個線程間的數據交換
案例
public class UseExchange {private static final Exchanger<Set<String>> exchange = new Exchanger<Set<String>>();public static void main(String[] args) {//第一個線程new Thread(new Runnable() {@Overridepublic void run() {Set<String> setA = new HashSet<String>();//存放數據的容器try {/*添加數據* set.add(.....)* */setA = exchange.exchange(setA);//交換set/*處理交換后的數據*/} catch (InterruptedException e) {}}}).start();//第二個線程new Thread(new Runnable() {@Overridepublic void run() {Set<String> setB = new HashSet<String>();//存放數據的容器try {/*添加數據* set.add(.....)* set.add(.....)* */setB = exchange.exchange(setB);//交換set/*處理交換后的數據*/} catch (InterruptedException e) {}}}).start();} }總結
以上是生活随笔為你收集整理的Exchange的介绍及使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hive之数仓的分层及建模理论
- 下一篇: 学习Nging笔记