Java multithreading ~~~ Synchronous and asynchronous Fork/Join frameworks
In the Fork/Join framework, there are two ways to submit a task: synchronous and asynchronous. The previously used invokeAll () method is synchronous, that is, any
After the task is submitted, this method will not be returned until all tasks have been processed. Another way is to use the fork method, which is asynchronous. Also
After you submit the task, the fork method returns immediately. You can continue the following task. This thread will continue to run.
The following is an example of a program that queries files ending with logs on a disk to describe asynchronous usage.
Package com. bird. concursey. charpet8; import java. io. file; import java. util. arrayList; import java. util. list; import java. util. concurrent. forkJoinPool; import java. util. concurrent. recursiveTask; public class FolderProcessor extends RecursiveTask
> {Private static final long serialVersionUID = 1L; private String path; private String extension; public FolderProcessor (String path, String extension) {super (); this. path = path; this. extension = extension;} @ Overrideprotected List
Compute () {List
List = new ArrayList
(); List
Tasks = new ArrayList
(); File file = new File (path); File content [] = file. listFiles (); if (content! = Null) {for (int I = 0; I <content. length; I ++) {if (content [I]. isDirectory () {FolderProcessor task = new FolderProcessor (content [I]. getAbsolutePath (), extension); // submit a task asynchronously. fork (); tasks. add (task);} else {if (checkFile (content [I]. getName () {list. add (content [I]. getAbsolutePath () ;}}} if (tasks. size ()> 50) {System. out. printf ("% s: % d tasks ran. \ n ", file. getAbsolutePath (), tasks. size ();} addResultsFromTasks (list, tasks); return list;}/*** that will add to the list of filesthe results returned by the subtasks launched by this task. * @ param list * @ param tasks */private void addResultsFromTasks (List
List, List
Tasks) {for (FolderProcessor item: tasks) {list. addAll (item. join ());}} /*** This method compares if the name of a filepassed as a parameter ends with the extension you are looking for * @ param name * @ return */private boolean checkFile (String name) {return name. endsWith (extension);} public static void main (String [] args) {ForkJoinPool pool = new ForkJoinPool (); FolderProcessor system = new FolderProcessor ("C: \ Windows ", "log"); FolderProcessor apps = new FolderProcessor ("C: \ Program Files", "log" contains multiple pool.execute(systemworkflow workflow pool.exe cute (apps); pool. shutdown (); List
Results = null; results = system. join (); System. out. printf ("System: % d files found. \ n ", results. size (); results = apps. join (); System. out. printf ("Apps: % d files found. \ n ", results. size ());}}
The key of this example is in the FolderProcessor class. Each task processes the content
Of a folder. As you know, this content has the following two kinds of elements:
Ff Files
Ff Other folders
If the task finds a folder, it creates another Task object to process that folder and sends it
The pool using the fork () method. This method sends the task to the pool that will execute it
If it has a free worker-thread or it can create a new one. The method returns immediately, so
The task can continue processing the content of the folder. For every file, a task compares its
Extension with the one it's looking for and, if they are equal, adds the name of the file to
List of results.
Once the task has processed all the content of the assigned folder, it waits for the finalization
Of all the tasks it sent to the pool using the join () method. This method called in a task
Waits for the finalization of its execution and returns the value returned by the compute ()
Method. The task groups the results of all the tasks it sent with its own results and returns
That list as a return value of the compute () method.
The ForkJoinPool class also allows the execution of tasks in an asynchronous way. You
Have used the execute () method to send the three initial tasks to the pool. In the Main
Class, you also finished the pool using the shutdown () method and wrote information about
The status and the evolution of the tasks that are running in it. The ForkJoinPool class
Except des more methods that can be useful for this purpose. See the Monitoring a Fork/Join
Pool recipe to see a complete list of those methods.