Java concurrency (basic knowledge)-blocking queue and producer consumer Mode

Source: Internet
Author: User

Java concurrency (basic knowledge)-blocking queue and producer consumer Mode
1. Blocking Queue BlockingQueue is a thread-safe Queue version. It can be seen from its name that it supports blocking Queue implementation: When requesting data from an empty BlockingQueue, it will block BlockingQueue to a non-empty value. When inserting data into a full BlockingQueue, the thread will block BlockingQueue to be inserted. BlockingQueue methods appear in four forms. for operations that cannot be met immediately but may be satisfied at a certain time in the future, these four forms of processing are different: the first is to throw an exception, the second is to return a special value (null or false, depending on the operation), and the third is to block the current thread indefinitely before the operation is successful, the fourth approach is to block the service only within the given maximum time limit before giving up. The following table summarizes these methods: throwing an exception special value blocking timeout insert add (e) offer (e) put (e) offer (e, time, unit) remove () poll () take (time, unit) checks that element () peek () BlockingQueue does not accept null elements. When you try to add, put, or offer a null element, some implementations will throw NullPointerException. Null is used as a warning value to indicate that the poll operation fails. BlockingQueue can be a limited capacity. It can have a remainingCapacity at any given time, beyond which additional elements cannot be put without interruption. BlockingQueue without any internal capacity constraints always reports the remaining capacity of Integer. MAX_VALUE. JDK provides the following blocking queue implementations: ArrayBlockingQueue: A Bounded blocking queue consisting of arrays. LinkedBlockingQueue: A Bounded blocking queue composed of linked lists. PriorityBlockingQueue: an unbounded blocking queue that supports priority sorting. DelayQueue: an unbounded blocking queue that can retrieve data only when the delay expires. SynchronousQueue: a blocking queue that does not store elements. 2. The producer consumer mode blocks the queue and supports the producer-consumer mode. This mode separates the two processes: "finding the work to be done" and "execution work, and put the work item in a "to be completed" list for subsequent processing, rather than processing immediately after finding out. The producer-consumer model simplifies the development process because it eliminates code dependencies between the producer class and the consumer class. In addition, this mode also decouples the process of producing data from the process of using data to simplify workload management because these two processes have different data processing speeds. In the producer-consumer design built based on the blocking queue, when data is generated, the producer puts the data into the queue, and when the consumer is preparing to process the data, the data will be obtained from the queue. The producer does not need to know the consumer ID or quantity, or whether they are unique producers. Instead, they only need to put the data in the queue. Likewise, consumers do not need to know who the producer is or where the job comes from. BlockingQueue simplifies the implementation process of producer-consumer design. It supports any number of producers and consumers. A common producer-consumer design mode is the combination of a thread pool and a work queue. This mode is embodied in the Executor task execution framework. The blocking queue simplifies the encoding of the producer-consumer mode, because the take operation will be blocked until available data is available. If the producer cannot produce work items as soon as possible, keep the consumer busy, then the consumer can only wait until there is work to do. Similarly, the put operation can also simplify encoding. If a blocking queue is used, when the queue is full, the producer will be blocked and cannot continue to work, so that the consumer will have time to catch up with the producer's speed. 3. Desktop Search examples there is a type of program suitable for splitting into producers and consumers, such as a proxy program that will scan files on a local drive and resume indexes for subsequent searches, similar to some desktop search programs. In the following code, the crawler thread provides a producer task, that is, to search for files that meet the index standards in a file hierarchy and put their names in the working queue. A consumer task is provided in IndexerThread, that is, getting the file name from the queue and indexing them. The example is from Java concurrent programming practice. Class crawler Thread extends Thread {private final BlockingQueue <File> fileQueue; private final File root; public crawler Thread (BlockingQueue <File> fileQueue, File root) {super (); this. fileQueue = fileQueue; this. root = root;} public void run () {try {crawl (root);} catch (InterruptedException e) {Thread. currentThread (). interrupt () ;}} private void crawl (File root) throws InterruptedException {File [] entries = root. listFiles (); if (entries! = Null) {for (File entry: entries) {if (entry. isDirectory () crawl (entry); else if (! FileQueue. contains (entry) fileQueue. put (entry) ;}}} class IndexerThread extends Thread {private final BlockingQueue <File> queue; public IndexerThread (BlockingQueue <File> queue) {super (); this. queue = queue;} public void run () {try {while (true) {indexFile (queue. take () ;}} catch (InterruptedException consumed) {Thread. currentThread (). interrupt () ;}} public void indexFile (File file ){/ *... */};} The producer-consumer mode provides a method suitable for threads to break down Desktop Search problems into simpler components, splitting file traversal and indexing into independent operations is more readable and reusable than putting all functions into one operation. Each operation only needs to complete one task, blocking queues are responsible for all the control flows, so the code for each function is simpler and clearer. The producer-consumer model also brings many performance advantages. Producers and consumers can execute concurrently. If one is I/O-intensive and the other is CPU-intensive, the throughput of concurrent execution is higher than that of serial execution. If the parallelism between producers and consumers is different, coupling them will reduce the overall parallelism to a smaller degree of parallelism. Run the following code to start multiple search threads and index threads: public static void main (String [] args) {final int N_CONCUMERS = 10; File [] roots = ...; blockingQueue <File> queue = new LinkedBlockingQueue <File> (1000); for (File root: roots) new crawler thread (queue, root ). start (); for (int I = 0; I <N_CONCUMERS; I ++) new IndexerThread (queue ). start ();}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.