zoukankan      html  css  js  c++  java
  • java高级---->Thread之BlockingQueue的使用

      今天我们通过实例来学习一下BlockingQueue的用法。梦想,可以天花乱坠,理想,是我们一步一个脚印踩出来的坎坷道路。

    BlockingQueue的实例

    官方文档上的对于BlockingQueue的说明:

    A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

    如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤醒.同样,如果BlockingQueue是满的,任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有空间才会被唤醒继续操作

    一、BlockingQueue的简单使用

    package com.linux.huhx.concurreny;
    
    import java.util.Random;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingDeque;
    import java.util.concurrent.TimeUnit;
    
    public class BlockingQueueTest {
        public static void main(String[] args) {
            BlockingQueue q = new LinkedBlockingDeque();
            Producer p = new Producer(q);
            Consumer c1 = new Consumer(q);
            Consumer c2 = new Consumer(q);
            new Thread(p).start();
            new Thread(c1).start();
            new Thread(c2).start();
        }
    
        static class Producer implements Runnable {
            private final BlockingQueue<String> queue;
            Producer(BlockingQueue<String> queue) {
                this.queue = queue;
            }
            @Override
            public void run() {
                for (int i = 0; i < 4; i++) {
                    try {
                        queue.put("producer" + i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        static class Consumer implements Runnable {
            private final BlockingQueue queue;
            Consumer(BlockingQueue queue) {
                this.queue = queue;
            }
            public void run() {
                for (int i = 0; i < 2; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(new Random().nextInt(2000));
                        System.out.println(queue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    运行的结果如下:使用put和take方法,打印的结果是固定的。它会阻塞。

    producer0
    producer1
    producer2
    producer3

    二、ArrayBlockIngQueue的使用

    package com.linux.thread.thread;
    
    import org.junit.Test;
    
    import java.util.concurrent.ArrayBlockingQueue;
    
    public class ArrayBlockQueueTest {
        @Test
        public void put() {
            try {
                ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
                queue.put("huhx");
                queue.put("linux");
                queue.put("ll");
                System.out.println("size: " + queue.size());
                System.out.println("begin: " + System.currentTimeMillis());
                queue.put("tomhu"); // 阻塞
                System.out.println("end: " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        @Test
        public void get() {
            try {
                ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
                System.out.println("begin: " + System.currentTimeMillis());
                System.out.println(queue.take()); // 阻塞
                System.out.println("end: " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    友情链接

  • 相关阅读:
    数全排列问题
    DNA repair问题
    分治问题
    贪心问题
    STL简单应用问题
    求一个数阶乘后位数问题
    《DSP using MATLAB》Problem 2.6
    《DSP using MATLAB》示例Example 10.4
    《DSP using MATLAB》Problem 2.5
    《DSP using MATLAB》Problem 2.4
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusejavaBlockingQueue.html
Copyright © 2011-2022 走看看