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
[[152236]] The annual Alibaba Cloud Conference at...
During the Spring Festival, everyone must have fo...
In the early stages of entrepreneurship , from bu...
NetEase Cloud Music was launched in April 2013, a...
How did Wu Chunhong receive more than 2.62 millio...
When we talk about e-commerce, what will be the f...
Recently, many places in my country have reported ...
【51CTO.com Quick Translation】Using the same passw...
What is the most important thing about Thai adver...
On January 6, the highly anticipated Big Chief ga...
I have the model in hand and the idea. If you don...
The Chinese names of plants are like refined code...
So far, we have given corresponding methods for e...
Starting from February 15, 2017, Beijing will imp...
As an important part of native advertising, infor...