Implementation of Java blocking queue thread set control

Source: Internet
Author: User

Queues manage data in a first-in, in-out manner. If you attempt to add an element to a blocked queue that is already full, or remove an element from an empty blocking queue, the thread will be blocked. Blocking queues is a useful tool when working with multiple threads. Worker threads can periodically store intermediate results in a blocking queue. Other worker threads take the intermediate results out and modify them in the future. The queue automatically balances the load. If the first thread set runs slower than the second, then the second line threads block when the result is waiting. If the first set of threads runs fast, then it waits for the second set of threads to catch up.

The following program shows how to use a blocking queue to control the set of threads. The program searches all files in a directory and all its subdirectories, and prints a list of files containing the specified keywords.

The Java.util.concurrent package provides 4 variants of the blocking queue: Linkedblockingqueue, Arrayblockingqueue, Priorityblockingqueue, and Delayqueue. We're using a arrayblockingqueue. Arrayblockingqueue is constructed with a given capacity and can choose whether fairness is required. If the fairness parameter is set, the thread waiting for the longest time will be processed first. In general, fairness makes you pay for performance, and you can use it only when you really need it.

The producer thread enumerates all the files in all subdirectories and places them in a blocking queue. This operation is fast, and if the queue is not capped, it will soon contain files that are not found.

We also launched a large number of search threads. Each search thread pulls a file out of the queue, opens it, prints out all the lines that contain the keyword, and then pulls out the next file. We used a little trick to terminate the thread at the end of the work. In order to emit a completion signal, the enumeration thread puts a virtual object into the queue. (This is similar to putting a virtual package with a "last packet" on the baggage conveyor.) When the search thread is taken to the virtual object, it is put back and terminated.

Note that there is no need for anyone to display the thread synchronization. In this program, we use the queue data structure as a synchronization mechanism.

Import java.io.*;  Import java.util.*;  Import java.util.concurrent.*; public class Blockingqueuetest {public static void main (string[] args) {Scanner in = new Scanner (System.        in);        System.out.print ("Enter base directory (e.g./usr/local/jdk1.6.0/src):");        String directory = In.nextline ();        System.out.print ("Enter keyword (e.g. volatile):");        String keyword = in.nextline ();        Final int file_queue_size = 10;        Final int search_threads = 100;        blockingqueue<file> queue = new arrayblockingqueue<file> (file_queue_size);        Fileenumerationtask enumerator = new Fileenumerationtask (queue, new File (directory));        New Thread (Enumerator). Start ();     for (int i = 1; I <= search_threads; i++) New Thread (New Searchtask (queue, keyword)). Start ();   }}/** * This task enumerates all files in a directory and its subdirectories. */class Fileenumerationtask implements Runnable {/** * ConsTructs a fileenumerationtask.  * @param queue the blocking queue to which the enumerated files is added * @param startingdirectory the directory in Which to start the enumeration */public Fileenumerationtask (blockingqueue<file> queue, File startingdirect        Ory) {this.queue = queue;     This.startingdirectory = startingdirectory;           } public void Run () {try {enumerate (startingdirectory);        Queue.put (DUMMY);  } catch (Interruptedexception e) {}}/** * Recursively enumerates all files in a given Directory and its subdirectories * @param directory of the directory in which to start */public void Enumerate        (File directory) throws interruptedexception {file[] files = directory.listfiles ();           for (file file:files) {if (File.isdirectory ())) enumerate (file);        else Queue.put (file); }} public static File DUMMY = new File ("");     Private blockingqueue<file> queue;  Private File startingdirectory;   }/** * This task searches the files for a given keyword.      */class Searchtask implements Runnable {/** * constructs a searchtask. * @param queue the queue from which to take files * @param keyword the keyword to look for */public Searchta        SK (blockingqueue<file> queue, String keyword) {this.queue = queue;     This.keyword = keyword;           public void Run () {try {boolean-done = false;              while (!done) {File File = Queue.take ();                 if (file = = Fileenumerationtask.dummy) {queue.put (file);              Done = true;                       } else search (file);        }} catch (IOException e) {e.printstacktrace (); } catch (Interruptedexception e) {}}     /** * Searches a file for a given keyword and prints all matching lines.  * @param file The file to search */public void search (file file) throws IOException {Scanner in = new        Scanner (new FileInputStream (file));        int linenumber = 0;           while (In.hasnextline ()) {linenumber++;           String line = In.nextline (). Trim ();        if (line.contains (keyword)) System.out.printf ("%s:%d%s%n", File.getpath (), linenumber, line);     } in.close ();     } private blockingqueue<file> queue;  Private String keyword; }

Implementation of the Java blocking queue thread set control

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.