JUC个人学习笔记8---阻塞队列

根据b站UP主狂神说JUC课程所写的个人学习笔记

视频地址:https://www.bilibili.com/video/BV1B7411L7tE?from=search&seid=14761503393031794075


阻塞

队列

阻塞队列:

BlockingQueue 不是新的东西

什么时候会使用阻塞队列:

多线程并发处理,线程池

学会使用队列

1.添加2.移除

四组api

1.抛出异常

2.不会抛出异常

3.阻塞等待

4.超时等待

方式抛出异常不会抛出异常,有返回值阻塞等待超时等待
添加add()offer()put()offer(,,)
移除remove()poll()take()poll(,)
判断队列首部element()peek()  
public class BlockingQueueDemo {
    public static void main(String[] args) {
        //list
        //set
        //抛出异常
        test1();


    }
    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.remove());
        System.out.println(blockingqueue.remove());
        System.out.println(blockingqueue.remove());
        //java.util.NoSuchElementException
        //System.out.println(blockingqueue.remove());
    }
}

 

//有返回值,没有异常
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"));
    //System.out.println(blockingqueue.offer("d"));//false不抛出异常
    System.out.println(blockingqueue.poll());
    System.out.println(blockingqueue.poll());
    System.out.println(blockingqueue.poll());
    System.out.println(blockingqueue.poll());//null不抛出异常

}

 

//阻塞(一直)
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(blockingqueue.take());
    System.out.println(blockingqueue.take());
    System.out.println(blockingqueue.take());
    //System.out.println(blockingqueue.take());//一直阻塞


}
//阻塞(等待超时)
public static void test4() throws InterruptedException {
    ArrayBlockingQueue blockingqueue = new ArrayBlockingQueue<>(3);
    blockingqueue.offer("a");
    blockingqueue.offer("b");
    blockingqueue.offer("c");
    //blockingqueue.offer("d",2, TimeUnit.SECONDS);//等待超过2s就退出
    System.out.println(blockingqueue.poll());
    System.out.println(blockingqueue.poll());
    System.out.println(blockingqueue.poll());
    blockingqueue.poll(2,TimeUnit.SECONDS);

}

 


同步队列

SynchronousQueue

没有容量,进去一个元素,必须等待取出之后才能再往里面放一个元素

put,take

public class SynchronousQueueDemo {
    public static void main(String[] args) {
        SynchronousQueue <String> synchronousQueue = new SynchronousQueue<String>();//同步队列
        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();
            }

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


        },"t2").start();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值