天天看點

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();

    }


}

      

先啟動消費者,隻要生産者一生産消息,就會取到: