JUC-阻塞队列入门

/**

  • @Author: CYW
  • @Date: 2020/11/19 21:42
  • 阻塞队列BlockingQueue
  • Queue的结构
  •  Collection
    
  •      -Queue
    
  •          -Deque
    
  •          -AbstractQueue
    
  •          -BlockingQueue
    
  •                -LinkedBlockingQueue
    
  •                -ArrayBlockingQueue
    
  •          层层递进,下面的是上面的孩子
    

*/
public class BlockingQueueTest {
public static void main(String[] args) throws InterruptedException {
//test01();
//test02();
//test03();
test04();
}

/**
 * 第一种:队列已满(加不进去)或队列已空(取不出来),
 * 抛出异常,添加add(),移出remove()
 */
public static void test01(){
    //设置队列长度为3
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
    System.out.println(blockingQueue.add("a"));
    System.out.println(blockingQueue.add("b"));
    System.out.println(blockingQueue.add("c"));
    //System.out.println(blockingQueue.add("d"));//抛出异常,因为队列已满

    System.out.println(blockingQueue.remove());//出队,返回出队的值,FIFO
    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());
    //System.out.println(blockingQueue.remove());//队列已空,抛出异常

}

/**
 * 第二种:队列已满(返回false)或队列已空(返回null),
 * 添加offer(),移出poll(),peek()检测队首元素是什么
 */
public static void test02(){
    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.peek());//检测队首元素

    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    //System.out.println(blockingQueue.poll());//队列已空,返回null
}

/**
 * 第三种:队列已满(一直等待)或队列已空(一直等待),
 * 等待阻塞
 * 添加put(),移出take()
 */
public static void test03() 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());//队列已空,一直等待,程序不停
}

/**
 * 第四种:队列已满(超时等待,超时退出),队列已空(超时等待,超时退出),
 * 添加offer(),移出poll(int a,TimeUnit,SECONDS)
 */
public static void test04() 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);//等待两秒钟队列还是没位置就不等

    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    blockingQueue.poll(2,TimeUnit.SECONDS);//等待两秒钟队列依然为空就不等了
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT二叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值