1. What is Fork/JoinThe official definition given by Oracle is: Fork/Join framework is a multi-threaded processor that implements the ExecutorService interface. It can divide a large task into several small tasks for concurrent execution, making full use of available resources, thereby improving the execution efficiency of the application. Fork/Join implements ExecutorService, so its tasks also need to be executed in the thread pool. The difference is that it uses a work stealing algorithm, where idle threads can steal tasks from fully loaded threads to help execute. (My personal understanding of work stealing is: since each thread in the thread pool has a queue, and the threads do not affect each other. So each thread gets a task from the head of its own task queue to execute. If a thread's task queue is empty at some point, and there are still tasks in the task queues of other threads, then this thread will take a task from the task queues of other threads to help execute. It's like stealing other people's work) The core of the Fork/Join framework is the ForkJoinPool class that inherits AbstractExecutorService, which ensures the normal operation of the work-stealing algorithm and ForkJoinTask. The following is the original text quoted from Oracle's official definition:
2. Basic usage of Fork/Join(1) Fork/Join base classAs mentioned above, Fork/Join is about splitting a large task into several small tasks, so the first step is of course to split the tasks, roughly as follows:
To implement FrokJoinTask, we need a base class that inherits RecursiveTask or RecursiveAction, and put the above code into the coupute method of the base class according to our business situation. Both RecursiveTask and RecursiveAction inherit FrokJoinTask. The difference between them is that RecursiveTask has a return value while RecursiveAction does not. The following is a demo I made to select the elements that still have "a" in a string list:
(2) Execution classAfter the base class is completed, you can start calling. When calling, we first need the Fork/Join thread pool ForkJoinPool, then submit a ForkJoinTask to the thread pool and get the result. The input parameter of the submit method of ForkJoinPool is a ForkJoinTask, and the return value is also a ForkJoinTask. It provides a get method to get the execution result. The code is as follows:
In this way, we have completed the development of a simple Fork/Join. Tip: In Java 8, the parallelSort() method of java.util.Arrays and the encapsulated methods in the java.util.streams package also use Fork/Join. (Careful readers may notice that I also use stream in Fork/Join, so this Fork/Join is actually redundant because stream has already implemented Fork/Join. However, this is just a demo display and it doesn’t matter if it has no practical use.) Quoting the official text:
Attached is the complete code for future reference:1. Define an abstract class (for expansion, which has no practical use in this example and can be left undefined):
2. Define the base class
3. Execution class
|
<<: DeepMind: Combining artificial intelligence and neuroscience to achieve a virtuous cycle
>>: Aiti Tribe Story Collection (25): Analysis of Chain Storage Stack and Code Implementation
Alcohol consumption is one of the leading causes ...
First of all, we must understand that any plan mu...
Many people have some moles on their bodies or fa...
Event operations play an important role in the op...
Training course content: 50 million big Douyin ac...
What is the significance of Google's artifici...
I have to say that’s a great question and one I’m...
Michael Walkley, an analyst at U.S. investment fi...
The establishment of an effective user segmentati...
On March 2, 2016, the National Office for Combatin...
Not long ago, on World Intellectual Property Day,...
Although Gree's acquisition of Zhuhai Yinlong...
The first step in advertising is to gain exposure...
As a legal citizen, we must not only learn to abi...
Course Catalog Brushes and Materials 18. Elf Hams...