zoukankan      html  css  js  c++  java
  • java 多线程 day17 Exchanger



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

    public class Thread1701_Exchanger {

    public static void main(String[] args) {
    ExecutorService service = Executors.newCachedThreadPool();
    final Exchanger exchanger = new Exchanger();
    service.execute(new Runnable(){
    public void run() {
    try {

    String data1 = "zxx";
    System.out.println("线程" + Thread.currentThread().getName() +
    "正在把数据" + data1 +"换出去");
    Thread.sleep((long)(Math.random()*10000));
    String data2 = (String)exchanger.exchange(data1);
    System.out.println("线程" + Thread.currentThread().getName() +
    "换回的数据为" + data2);
    }catch(Exception e){

    }
    }
    });
    service.execute(new Runnable(){
    public void run() {
    try {

    String data1 = "lhm";
    System.out.println("线程" + Thread.currentThread().getName() +
    "正在把数据" + data1 +"换出去");
    Thread.sleep((long)(Math.random()*10000));
    String data2 = (String)exchanger.exchange(data1);
    System.out.println("线程" + Thread.currentThread().getName() +
    "换回的数据为" + data2);
    }catch(Exception e){

    }
    }
    });
    }
    }
  • 相关阅读:
    Python(93)_网络编程基础
    Web前端(13)_路径
    Web前端(12)_注释
    Web前端(11)_base标签
    Python(91)_python复习
    Web前端(10)_css文本样式
    Web前端(9)_div+span等标签
    虚拟化(6)_CPU虚拟化
    虚拟化(5)_打开tcp监听
    虚拟化(4)_离线克隆与在线克隆
  • 原文地址:https://www.cnblogs.com/ctaixw/p/7980995.html
Copyright © 2011-2022 走看看