Corejava_ thread concurrency (blocked queue): Search for a file containing a keyword in a directory

Source: Internet
Author: User

Java multithreaded programming is a test of a program ape level.

In a traditional Web program. Because the framework provides too much robustness, concurrency, and reliability support, we are all focused on the business implementation. We do not, however, follow the requirements of business logic. Keep accumulating your own code.

Due to knowledge, or limitations of experience. There are often problems without knowing them.

For example, some of the more original items. It does not use a relatively flexible and robust framework such as spring.

Instead, the servlet is used only as a way to implement the service side.


To lift a simple chestnut. It is well known that when a container is requested, the container is created and a servlet thread is started to correspond to the current request, at this time. Member variables in a servlet are affected by multithreading. In this way, it becomes critical to write the member variable code in the servlet. Because after all, not a thread-safe design. Multi-threaded synchronization, mutual exclusion, competition, coordination, and the interruption of threads have become our coding process. A point of knowledge that is very important.


In Java core technology. When it comes to concurrency control for multithreading: First, we recommend using a blocking queue. A blocking queue is a data structure that is professionally thread-safe. We are programmed on this, just as we stand on the shoulders of giants, and the very many low-level implementations are the things they consider. Secondly, if you want to control multithreading concurrency, as far as possible to recommend the use of Synchronizedkeyword,synchronizedkeyword is actually equivalent to Reentrantlock or read-write lock implementation, but the syntax is more concise. Not too easy to make mistakes. FINALLY. Finally, if there are sufficient reasons, it is recommended to use Reentrantlock or Reentrantreadwritelock, Readlock, Writelock and its corresponding condition bar, volatile, etc. Since these are too low-level, and according to Java's consistent implementation of the coding logic, we just need to know that a control thread concurrency data structure exists.


The following is a very good one in the core technology. A demo that can be used to study blocked queues. The purpose is to be used below a given directory. Search for all files that contain a keyword, which is implemented in more detail under very many editors. such as UE.


The way the feature is implemented is probably this: the producer thread, putting all the files below the current given directory into the blocking queue (which uses recursion in the feature implementation), and finally, placing an empty file as a flag. Similar to the way a semaphore looks. Creates very many consumer threads, loops through the block queue, fetches a file, and then walks through it, assuming it contains keyword to print. The condition to jump out of the loop is to see the lights placed by the consumer thread. The implementation of the feature will depend concurrently on a concurrent data structure. Very worthwhile to learn from.

Import Java.io.file;import java.io.fileinputstream;import Java.io.ioexception;import Java.util.Scanner;import Java.util.concurrent.arrayblockingqueue;import Java.util.concurrent.blockingqueue;public class BlockingQueueTest { public static void Main (string[] args) {Scanner in = new Scanner (system.in); System.out.println ("Enter base directory (eg. /USR/LOCAL/JDK1.6.0/SRC) "); String directory = In.nextline (); SYSTEM.OUT.PRINTLN ("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 ();}} Class Fileenumerationtask implements Runnable{public Fileenumerationtask (blockingqueue<file> queue,File Startingdirectory) {This.queue=queue;this.startingdirectory = Startingdirectory;} @Overridepublic void Run () {try{enumerate (startingdirectory); Queue.put (DUMMY);} catch (Interruptedexception e) {}}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 ("");p rivate blockingqueue<file> queue;private file startingdirectory;} Class Searchtask implements Runnable{public searchtask (blockingqueue<file> queue,string keyword) {this.queue = Queue;this.keyword = keyword;} @Overridepublic void Run () {try {Boolean done = False;while (!done) {File File = Queue.take (); if (file==fileenumerationtask . DUMMY) {queue.put (file);d one=true;} else {search (file);}}}  catch (IOException e) {} catch (Interruptedexception e) {}}private void search (file file) throws Ioexception{scanner in = new Scanner (new FileInputStream (file)), int linenumber = 0;while (In.hasnextline ()) {linenumber++; String line = In.nextline(); if (line.contains (keyword)) System.out.printf ("%s:%d:%s%n", File.getpath (), linenumber,line);} In.close ();} Private blockingqueue<file> queue;private String keyword;}



Corejava_ thread concurrency (blocked queue): Search for a file containing a keyword in a directory

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.