天天看点

Java并发容器--LinkedTransferQueue

package com.zhangxueliang.demo.springbootdemo.normal;

import java.util.concurrent.LinkedTransferQueue;

/**
 * @ProjectName springbootdemo_src
 * @ClassName TransferQueue
 * @Desicription TODO
 * @Author Zhang Xueliang
 * @Date 2019/11/11 11:02
 * @Version 1.0
 **/
public class TransferQueue {

    public static void main(String[] args) throws Exception {
        LinkedTransferQueue<String> strs = new LinkedTransferQueue<>();
        /*new Thread(()->{
            try {
                System.out.println(strs.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();*/
        strs.transfer("aaa");//先启动生产者,如果没有消费者会一直阻塞
        new Thread(()->{
            try {
                System.out.println(strs.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }


}

      

先启动消费者,只要生产者一生产消息,就会取到: