唐僧喜欢小龙女

导航

BlockingQueue 学习

1、阻塞队列 以及常见的API

/**
 *
 *
 *   BlockingQueue 学习
 *   BlockingQueue 继承自Collection
 *   意思是 阻塞 和 队列
 *      阻塞就是等待的意思,当前线程就等待了,放不进去队列就阻塞,取不去来就阻塞
 *      队列的意思就是:先进先出啊。
 *   四组API的使用
 *
 *
 *
 */
public class Test {

    public static void main(String[] args) throws InterruptedException{
        Test test = new Test();
        test.test4();
    }

    /**
     *
     * 抛出异常的方式
     *
     */
    public void test1(){
        //队列的大小 只能放3个
        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("==================");


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

        //检测队首元素 这里抛出异常的时候测试的时候使用 这个方法
        System.out.println("目前队首的元素是"+blockingQueue.element());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        // java.util.NoSuchElementException
        //System.out.println(blockingQueue.remove());
    }

    /**
     *
     *  这里不抛出异常
     *
     */
    public void test2(){

        //队列的大小 只能放3个
       ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        //这里返回的是true
        System.out.println(arrayBlockingQueue.offer("c"));
        //这里返回的是false
        System.out.println(arrayBlockingQueue.offer("d"));


        System.out.println("=========================");
        System.out.println(arrayBlockingQueue.poll());
        //检测队首元素 这里不抛出异常的时候测试的时候使用 这个方法
        System.out.println("目前队首的元素是"+arrayBlockingQueue.peek());
        System.out.println(arrayBlockingQueue.poll());
        //这里返回的是 c
        System.out.println(arrayBlockingQueue.poll());
        //这里不抛出异常,直接返回的是null
        System.out.println(arrayBlockingQueue.poll());

    }

    //等待阻塞之一:一直阻塞
    public void test3() throws InterruptedException{
        //队列的大小 只能放3个
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        arrayBlockingQueue.put("a");  //没有返回值
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
       // arrayBlockingQueue.put("d");  //因为队列里面只能放3个元素,当前线程就一直阻塞在这里了
        System.out.println("队列中放完了");

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

        arrayBlockingQueue.take();    //没有返回值
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();    //因为队列中只有三个元素,取第四个的时后取不到当前线程就一直阻塞在这里了
        System.out.println("从队列中取完了");


    }

    // 当前线程阻塞几秒
    public void test4() throws InterruptedException{
        //队列的大小 只能放3个
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        //放的时候最多等两秒 如果两秒没有放进去当前线程就推出
        arrayBlockingQueue.offer("a",2, TimeUnit.SECONDS);
        arrayBlockingQueue.offer("b",2, TimeUnit.SECONDS);
        arrayBlockingQueue.offer("c",2, TimeUnit.SECONDS);
       // arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS);

        System.out.println("队列中放完了");

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

        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);  //取的时候取等两秒,如果取不到当前线程就退出
        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);
        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);
        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);
        System.out.println("队列中取完了");

    }

}

2、同步队列

/**
 *
 *  同步队列
 *
 *  SynchronousQueue 他不存储元素,和别的阻塞队列是不一样的。
 *  只要里面put了一个元素,必须先take 取出来否则不能put 进去元素。
 *  从下面的代码的执行结果也可以看出来,上面的结论是正确的。
 *
 *
 *
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue blockingDeque = new SynchronousQueue<String>();
        new Thread(()->{
            try{
                System.out.println("current thread is "+Thread.currentThread().getName());
                blockingDeque.put("1");


                System.out.println("current thread is "+Thread.currentThread().getName());
                blockingDeque.put("2");


                System.out.println("current thread is "+Thread.currentThread().getName());
                blockingDeque.put("3");


                System.out.println("current thread is "+Thread.currentThread().getName());
                blockingDeque.put("4");

            }catch (Exception e){

            }

        },"T1").start();


        new Thread(()->{
            try{

                TimeUnit.SECONDS.sleep(3);
                blockingDeque.take();
                System.out.println("current thread is "+Thread.currentThread().getName());

                TimeUnit.SECONDS.sleep(3);
                blockingDeque.take();
                System.out.println("current thread is "+Thread.currentThread().getName());

                TimeUnit.SECONDS.sleep(3);
                blockingDeque.take();
                System.out.println("current thread is "+Thread.currentThread().getName());

                TimeUnit.SECONDS.sleep(3);
                System.out.println("current thread is "+Thread.currentThread().getName());
                blockingDeque.take();


            }catch (Exception e){

            }


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

  

posted on 2021-08-22 15:50  与时具进&不忘初心  阅读(33)  评论(0编辑  收藏  举报