Brush interview questions 21: java concurrency utilities in the queue What?


! [image.png] (https://img2020.cnblogs.com/other/268922/202003/268922-20200330183801141-1514127119.png)

work queue thread pool java uses concurrent queue.
Queue is generally used in producer-consumer scenario, the process needs to wait in line.


Hello, I'm Li Fuchun, today's question is:

ConcurrentLinkedQueue and LinkedBlockingQueue What is the difference?


A: The safety of concurrent queue are provided java, provides operation of waiting, take, put differences are as follows:

file

Concurrent queue class hierarchy


Level is as follows:

image.png


Consumers examples producer

package org.example.mianshi.queue;

import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.stream.IntStream;

/**
 * 作者:     carter
 * 创建日期:  2020/3/30 17:39
 * 描述:     生产者消费者例子
 */

public class ConsumerProduceApp {

    public static void main(String[] args) {

        BlockingQueue<String> queue = new ArrayBlockingQueue<>(1000);

        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);

        producer.start();
        consumer.start();

        try {
            producer.join();
            consumer.join();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static class Consumer extends Thread {

        private BlockingQueue<String> blockingQueue;

        public Consumer(BlockingQueue<String> blockingQueue) {
            this.blockingQueue = blockingQueue;
        }

        @Override
        public void run() {

            String msg = "";
            do {
                try {
                    msg = blockingQueue.take();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(msg);

            } while (!Objects.equals(msg, "quit"));

        }
    }

    public static class Producer extends Thread {

        private BlockingQueue<String> blockingQueue;

        public Producer(BlockingQueue<String> blockingQueue) {
            this.blockingQueue = blockingQueue;
        }

        @Override
        public void run() {

            IntStream.rangeClosed(1, 100)
                    .forEach(i -> {
                        try {
                            blockingQueue.put("msg" + i);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    });

            try {
                blockingQueue.put("quit");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}


Bounded queue:

ArrayBlockingQueue with: a fixed capacity, contiguous memory queue

LinkedBlockingQueue: set the size of a bounded queue is not provided was unbounded queue

SychronousQueue: capacity is 0, take, put the operation waits each


unbounded queue:

PriorityQueue: priority unbounded queue

DelayedQueue: delayed unbounded queue

Select the queue



Depending on whether bounded: generally choose ArrayBlockingQueue, LinkedBlockingQueue 

in accordance with the memory tightness: ArrayBlockingQueue

According to throughput: LinkedBlockingQueue

Relay or less thread queue information High Performance: SynchronouseQueue


summary


This section provides a big opportunity ConcurrentBlockingQueue and LinkedBlockingQueue difference, then reviews the queue level java offer.


A simple example producers of consumer shows the basic use of the queue.


Finally, the choice of the queue under different scenarios.





image.png

The original is not easy, please indicate the source.

Guess you like

Origin www.cnblogs.com/snidget/p/12600362.html