并发包中的阻塞队列的应用

一、java5阻塞队列的应用

一个由数组支持的有界阻塞队列。此队列按 FIFO(先进先出)原则对元素进行排序。队列的头部 是在队列中存在时间最长的元素。队列的尾部 是在队列中存在时间最短的元素。新元素插入到队列的尾部,队列获取操作则是从队列头部开始获得元素


1.1 API

API 描述
ArrayBlockingQueue(int capacity) 创建一个带有给定的(固定)容量和默认访问策略的ArrayBlockingQueue。
ArrayBlockingQueue(int capacity, boolean fair) 创建一个具有给定的(固定)容量和指定访问策略的ArrayBlockingQueue。
ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) 创建一个具有定的(固定)容量和指定访问策略的 ArrayBlockingQueue,它最初包含给定 collection 的元素,并以 collection 迭代器的遍历顺序添加元素。

这里写图片描述


只有puttake 有阻塞功能


1.2 案例

案例一

2个线程不停的放,2个线程不停的取; 满了不能继续放,空了不能继续取

public class BlockingQueueTest {
    public static void main(String[] args) {
        final BlockingQueue queue = new ArrayBlockingQueue(3);
        for(int i=0;i<2;i++){
            new Thread(){
                public void run(){
                    while(true){
                        try {
                            Thread.sleep((long)(Math.random()*1000));
                            System.out.println(Thread.currentThread().getName() + "准备放数据!");                            
                            queue.put(1);
                            System.out.println(Thread.currentThread().getName() + "已经放了数据," +                           
                                        "队列目前有" + queue.size() + "个数据");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }

            }.start();
        }

        new Thread(){
            public void run(){
                while(true){
                    try {
                        //将此处的睡眠时间分别改为100和1000,观察运行结果
                        Thread.sleep(1000);
                        System.out.println(Thread.currentThread().getName() + "准备取数据!");
                        queue.take();
                        System.out.println(Thread.currentThread().getName() + "已经取走数据," +                           
                                "队列目前有" + queue.size() + "个数据");                    
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }.start();          
    }
}

案例二

案例: 阻塞队列实现同步通知的功能

public class BlockingQueueCommunication {

    /**
     * @param args 需要初始化,让其中一个先放入队列中。
     */
    public static void main(String[] args) {

        final Business business = new Business();
        new Thread(
                new Runnable() {

                    @Override
                    public void run() {

                        for(int i=1;i<=50;i++){
                            business.sub(i);
                        }

                    }
                }
        ).start();

        for(int i=1;i<=50;i++){
            business.main(i);
        }

    }

     static class Business {


          BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
          BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);

          {
              try {
                  System.out.println("xxxxxdfsdsafdsa");
                queue2.put(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }

          public  void sub(int i){
                try {
                    queue1.put(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                for(int j=1;j<=10;j++){
                    System.out.println("sub thread sequece of " + j + ",loop of " + i);
                }
                try {
                    queue2.take();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
          }

          public  void main(int i){
                try {
                    queue2.put(1);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                for(int j=1;j<=100;j++){
                    System.out.println("main thread sequece of " + j + ",loop of " + i);
                }
                try {
                    queue1.take();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
          }
      }

}

参考

  1. 传智多线程视频

猜你喜欢

转载自blog.csdn.net/qq_31156277/article/details/80516964
今日推荐