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
A wonderful debater teaches 12 lessons on precise...
Capture SSS-level Lingkun at the beginning, 5th t...
In this article, the author will analyze with you...
What? The image of the red-crowned crane has coll...
What kind of creative has a high click-through ra...
Crop straw It is the other half of agricultural p...
TOP9 How Baidu impressed users in its early days ...
Wash, wash, wash, have you washed the dishes from...
The rapid development of the Internet has also le...
BC Station’s latest practical SEO training case f...
Audit expert: Wang Guoyi Postdoctoral fellow in N...
Recently, the sky in Sioux Falls, South Dakota, U...
We all know by now how important social media is ...
When many companies apply for 400 telephone numbe...
Apple held its 2019 spring conference again, and ...