zoukankan      html  css  js  c++  java
  • Java并发:线程间数据传递和交换

    一、通过SynchronousQueue方式实现线程间数据传递:

      线程A与线程B共同持有一个SynchronousQueue的引用,线程B调用take方法,阻塞以等待; 线程A运行后计算出结果,将结果put到queue中;

    public class SynchronousQueueTest {
        public static void main(String[] args) throws InterruptedException {
    
            SynchronousQueue<Integer> queue = new SynchronousQueue<Integer>();
            //线程A putThread
            Thread putThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("put thread start");
                    try {
                        Thread.sleep(3000);
                        System.out.println("put thread put对象");
                        queue.put(1);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("put thread end");
                }
            });
            //线程B takeThread
            Thread takeThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("take thread start");
                    try {
                        System.out.println("take thread 等待put对象");
                        System.out.println("take from putThread: " + queue.take());
                    } catch (InterruptedException e) {
                    }
                    System.out.println("take thread end");
                }
            });
    
            putThread.start();
            takeThread.start();
        }
    }

     二、线程Exchanger工具类实现线程间的数据交换:

      当一个线程到达exchange调用点时,如果它的伙伴线程此前已经调用了此方法,那么它的伙伴会被调度唤醒并与之进行对象交换,然后各自返回。如果它的伙伴还没到达交换点,那么当前线程将会被挂起,直至伙伴线程到达——完成交换正常返回;或者当前线程被中断——抛出中断异常;又或者是等候超时——抛出超时异常。

    public class ExchangerTest {
    
        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 = "thread-1-data";
                        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 = "thread-2-data";
                        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){
                        
                    }                
                }    
            });        
        }
    }
  • 相关阅读:
    《网络对抗》 后门原理与实践
    《网络对抗》 逆向及Bof进阶实践
    20145211黄志远《网络对抗》Exp9 Web安全基础实践
    20145211 《网络对抗》Exp8 Web基础
    20145211黄志远 《网络对抗》Exp7 网络欺诈技术防范
    20145211MSF基础应用实验
    20145211黄志远 《网络对抗技术》 恶意代码分析
    20145211黄志远 《网络对抗技术》 免杀原理与实践
    20145211黄志远 《网络对抗技术》 后门原理与实践
    20145211《网络对抗》注入Shellcode并执行&&Return-to-libc攻击
  • 原文地址:https://www.cnblogs.com/java-zzl/p/9741288.html
Copyright © 2011-2022 走看看