zoukankan      html  css  js  c++  java
  • Exchanger

    exchanger用于两个线程交换数据

    /**
     * 两个线程交换数据,A线程发送数据给B线程,B线程接受的数据和A发送的数据是同一个对象
     *  A  will send  the Object java.lang.Object@6e22e576
     *  B  will send  the Object java.lang.Object@248711b7
     *  B  received the Object java.lang.Object@6e22e576
     *  A  received the Object java.lang.Object@248711b7
     */
    public class ExchangerTest {
    
        public static void main(String[] args) {
            final  Exchanger<Object> exchanger = new Exchanger<>();
    
            new Thread(() -> {
                Object obj = new Object();
                System.out.println( " A  will send  the Object " + obj);
                try {
                    Object rObj = exchanger.exchange(obj);
                    System.out.println( " A  received the Object " + rObj);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
    
            new Thread(() -> {
                Object obj = new Object();
                System.out.println( " B  will send  the Object " + obj);
                try {
                    Object rObj = exchanger.exchange(obj);
                    System.out.println( " B  received the Object " + rObj);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
  • 相关阅读:
    For each db / table
    转---网络上来的,做一个数组样的结构
    JAVA 相关资料
    转--也不知是哪位大侠写的了
    T-SQL切割字符串方法小结 2
    OPENQUERY
    行集函数专题
    行列转换
    第一章 SQL基础
    解释型语言与编译型语言的区别
  • 原文地址:https://www.cnblogs.com/moris5013/p/11879211.html
Copyright © 2011-2022 走看看