码迷,mamicode.com
首页 > 其他好文 > 详细

ExchangerDemo

时间:2018-06-03 17:37:48      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:cat   imp   static   err   str   mes   interview   bsp   pre   

package com.fh.interview;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author
 * @create 2018-06-03 下午4:31
 **/
public class ExchangerDemo {

    private static Exchanger<String> exchanger = new Exchanger();

    public static void main(String[] args) {
            ExecutorService pool = Executors.newFixedThreadPool(2);
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        String mess = exchanger.exchange("hi");
                        System.out.println("1:"+mess);
                    }catch (Exception e){

                    }
                }
            });
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        String aaa = exchanger.exchange("hello");
                        System.out.println("2"+aaa);
                    }catch (Exception e){

                    }
                }
            });
    }
}

 

ExchangerDemo

标签:cat   imp   static   err   str   mes   interview   bsp   pre   

原文地址:https://www.cnblogs.com/nihaofenghao/p/9129589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!