java基础3

java基础3

阻塞队列ArrayBlockingQueue

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FuvttpWS-1612011642946)(C:\Users\万年历1\AppData\Roaming\Typora\typora-user-images\image-20210130205924372.png)]

四组API

阻塞队列API特性入队出队头元素
不抛出异常offerpollpeek
阻塞等待puttake
超时异常offer(多参数)poll(含参数)
抛出异常addremoveelement
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class TestQueue {
    public static void main(String[] args) throws InterruptedException {
    	test1();
        System.out.println("===================");

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

        test3();
        System.out.println("===================");
        test4();
    }
    public static void test1(){
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue(3);

        System.out.println(blockingQueue.add('a'));
        System.out.println(blockingQueue.add('c'));
        System.out.println(blockingQueue.add('d'));

        //System.out.println(blockingQueue.add('e'));

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


        //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('c'));
        System.out.println(blockingQueue.offer('d'));

        System.out.println(blockingQueue.offer('e'));

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

        System.out.println(blockingQueue.peek());
        //System.out.println(blockingQueue.remove());
    }

    //等待,然后一直阻塞。
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue(3);

        blockingQueue.put('a');
        blockingQueue.put('c');
        blockingQueue.put('d');

        //一直阻塞,队列没有位子了。
        //blockingQueue.put('e');

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

        //System.out.println(blockingQueue.take());
        //System.out.println(blockingQueue.remove());
    }

    public static void test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue(3);

        System.out.println(blockingQueue.offer('a'));
        System.out.println(blockingQueue.offer('c'));
        System.out.println(blockingQueue.offer('d'));

        //队列阻塞,等待两秒后就停止写入。
        System.out.println(blockingQueue.offer('e',2,TimeUnit.SECONDS));

        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //读取数据,等待超过两秒就退出。
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));

        System.out.println(blockingQueue.peek());
        //System.out.println(blockingQueue.remove());
    }
}

运行结果:

true
true
true
a
a
c
d
===================
true
true
true
false
a
a
c
d
null
===================
a
a
c
d
===================
true
true
true
false
a
a
c
d
null
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值