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
2020 is the year when online consumer demand has ...
This article is the on-site dry goods of WOT2016 ...
Salinization is known as a stubborn disease of th...
The digital advertising landscape in 2022 (Part 1...
There is a strange man in the Guangzhou R&D de...
The TV cabinet at home is always messy and cannot...
Course Catalog 01. Why should we focus on the &qu...
The user social growth system of "Growth Cof...
Audi predicts that the way cars are manufactured ...
Film has always been one of the industries closes...
How to create an event that will go viral on WeCh...
Jia Zhen's "Little Red Book Merchant Cam...
Recently, Li Xiang, CEO of Ideal Auto, sent an in...
A bird's eye view of science Quanzhou Associa...
In the turbulent July, the long-dormant Internet ...