其他分享
首页 > 其他分享> > 阻塞队列

阻塞队列

作者:互联网

和队列的offer和poll不同
阻塞队列是put和take

停摆之前操作

只能在take之前去操作 不然就完蛋
如果第二个take写在新线程之前,自己的两个打印都会卡住

class XXX{
    static BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(3);
    public static void main(String[] args) throws InterruptedException {
        blockingQueue.put(1);
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        new Thread(()->{
            try {
                Thread.sleep(100);
                blockingQueue.put(2121);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
//        1
//        新开线程操作
//        添加完成
//        2121
}

标签:队列,System,阻塞,take,put,InterruptedException,blockingQueue
来源: https://www.cnblogs.com/purexww/p/15249904.html