JUC-BlockingQueue

阻塞队列BlockingQueue

四组API

抛出异常

不抛出异常,有返回值

阻塞

超时等待

添加

add()

offer()

put()

offer(,,)

删除

remove()

poll()

take()

poll(,)

查看队首元素

element()

peek()

public class BlockingQueueTest {

    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    /**
     * 抛出异常
     * add()
     * remove()
     */
    public static void test1() {
        // 设置阻塞队列大小为3
        BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);

        blockingQueue.add("a");
        blockingQueue.add("b");
        blockingQueue.add("c");
        // java.lang.IllegalStateException: Queue full,抛异常
        //blockingQueue.add("d");

        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
        // java.util.NoSuchElementException,抛异常
        blockingQueue.remove();
    }

    /**
     * 不抛出异常,有返回值 true 或 false
     * offer()
     * poll()
     * @throws InterruptedException
     */
    public static void test2() throws InterruptedException {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        System.out.println(blockingQueue.offer("d")); // false

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }

    /**
     * 阻塞一直等待
     * put()
     * take()
     * @throws InterruptedException
     */
    public static void test3() throws InterruptedException {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
//        blockingQueue.put("d"); // 无返回值,一直阻塞,等待

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take()); // 阻塞,一直等待
    }

    /**
     * 超时等待
     * offer(,,)
     * poll(,)
     * @throws InterruptedException
     */
    public static void test4() throws InterruptedException {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("d");
        blockingQueue.offer("d", 1, TimeUnit.SECONDS); // 阻塞,超时等待

        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS)); // null
    }
}

同步队列SynchronousQueue

public class SynchronousQueueTest {
    public static void main(String[] args) {
        // 同步队列,不会存储元素,put一个元素之后就会等待take
        // take一个元素就会等待put
        BlockingQueue<String> synchronousQueue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName() + " put 1");
                synchronousQueue.put("1");
                System.out.println(Thread.currentThread().getName() + " put 2");
                synchronousQueue.put("2");
                System.out.println(Thread.currentThread().getName() + " put 3");
                synchronousQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()-> {
            try {
                System.out.println(Thread.currentThread().getName() + "=>" + synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + "=>" + synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + "=>" + synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

内容来自狂神JUC课程

#JUC#
全部评论

相关推荐

06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务