阻塞队列BlockingQueue (BlockingQueue)

导读:本篇文章讲解 阻塞队列BlockingQueue (BlockingQueue),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

阻塞队列BlockingQueue (BlockingQueue)

在这里插入图片描述
在这里插入图片描述
什么情况下我们会使用阻塞队列:多线程并发处理。线程池;

学会使用队列;
添加,移除
在这里插入图片描述

1:抛出异常

 /**
     * 1:抛出异常
     */
    public static void test1(){

        //队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));

//        java.lang.IllegalStateException: Queue full 抛出异常
//        System.out.println(blockingQueue.add("d"));

        System.out.println(blockingQueue.element());//对首

        System.out.println("=======================");

        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());

//        java.util.NoSuchElementException  抛出异常
//        System.out.println(blockingQueue.remove());

    }

2:不抛出异常

/**
 * 2;有返回值,没有异常
 */

public static void test2(){

    //队列的大小
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

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

    //返回一个Boolean值; false;
    System.out.println(blockingQueue.offer("d"));

    System.out.println(blockingQueue.peek()); //对首
    System.out.println("===========================");

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

    //返回一个Null;
    System.out.println(blockingQueue.poll());


}

3:阻塞等待

/**
 *3:等待  阻塞(一直阻塞)
 */

public static void test3() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    //一直阻塞
    blockingQueue.put("a");
    blockingQueue.put("b");
    blockingQueue.put("c");

   // blockingQueue.put("d");//队列没有位置了

    System.out.println("=========================");

    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());

    //System.out.println(blockingQueue.take());//没有这个元素,一直阻塞



}

4:超时等待

/**
 *4:等待  阻塞(等待超时)
 */
public static void test4() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    System.out.println(blockingQueue.offer("a"));
    System.out.println(blockingQueue.offer("b"));
    System.out.println(blockingQueue.offer("c"));
    blockingQueue.offer("d",2,TimeUnit.SECONDS);//等待超时 2 秒退出

    System.out.println("=====================");

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

SynchronousQueue 同步队列

package com.baidu.BlockingQueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * 同步队列:
 * 和其他的BlockingQueue  不一样,SynchronousQueue 不存储元素;
 * put 一个元素必须从里面先take 取出来,否则不能put 进入里面的值
 */
public class SynchronousQueueDemo {

    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

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

        },"T1").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());

                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());

                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"T2").start();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/71873.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!