Java Multithreading-Topics-chat concurrency (eight)--fork/join Framework Introduction

Source: Internet
Author: User

This article starts in Infoq 1. What is the Fork/join framework

The Fork/join framework is a framework that JAVA7 provides for the parallel execution of tasks, a framework that divides large tasks into small tasks and eventually results in large tasks with the results of each small task.

We then fork and join these two words to understand the fork/join framework, fork is a large task cut into a number of subtasks parallel execution, join is to merge the implementation of these subtasks, and finally get the results of this big task. For example, calculate 1+2+. +10000, can be divided into 10 subtasks, each subtask to the 1000 number of the sum, the final summary of the results of the 10 subtasks. Fork/join's running flowchart is as follows:


2. Work Stealing algorithm

The work theft (work-stealing) algorithm is a thread that steals tasks from other queues to execute. The running flowchart for work theft is as follows:

So why do you need to use a work-stealing algorithm? If we need to do a larger task, we can split this task into a number of interdependent subtasks, in order to reduce the competition between the threads, so put these subtasks into separate queues, and create a separate thread for each queue to perform the task in the queue, thread and queue one by one correspond, For example, a thread is responsible for handling tasks in a queue. However, some threads will finish the task in their own queue, while other threads have tasks waiting to be processed in the queue. Instead of waiting for the thread to work, it is better to help other threads, so it steals a task from the queue of other threads to execute. And at this point they will access the same queue, so in order to reduce the competition between the stealing task thread and the stolen task thread, the two-terminal queue is usually used, the stolen task thread always takes the task from the head of the two-end queue, and the thread that steals the task executes the task from the tail of the two-end queue forever.

The advantage of a work-stealing algorithm is that it makes full use of threads for parallel computing and reduces the competition between threads, with the disadvantage that in some cases there is competition, such as when there is only one task in a two-terminal queue. and consumes more system resources, such as creating multiple threads and multiple two-terminal queues. 3. Introduction to the Fork/join framework

We are already aware of the need for the Fork/join framework, so we can think about how to design a fork/join framework if we want to design it. This thinking helps you understand the design of the Fork/join framework.

The first step is to split the task. First we need to have a fork class to split the big task into subtasks, it is possible that the subtasks are still very large, so still need to continue to split until the sub task is small enough to split.

The second step executes the task and merges the results. The split subtasks are placed in a two-terminal queue, and several boot threads get task execution from the two-terminal queue respectively. The results of the subtasks are uniformly placed in a queue, starting a thread to take the data from the queue, and then merging the data.

Fork/join uses two classes to accomplish these two things: Forkjointask: To use the Forkjoin framework, you must first create a forkjoin task. It provides a mechanism for performing Fork () and join () operations in a task, usually without inheriting the Forkjointask class directly, but only inheriting its subclasses, and the Fork/join framework provides the following two subclasses: Recursiveaction: For tasks that do not return results. Recursivetask: For tasks that have return results. Forkjoinpool:forkjointask needs to be performed through Forkjoinpool, and subtasks are added to the two-terminal queues maintained by the current worker thread to the head of the queue. When a worker thread does not have a task in its queue, it randomly obtains a task from the tail end of the queue of other worker threads. 4. Using the Fork/join framework

Let's use the Fork/join framework with a simple requirement: compute the results of 1+2+3+4.

The first thing to consider when using the Fork/join framework is how to split a task, and if we want each subtask to perform a maximum of two numbers, then we set the threshold for the split to be 2, because the 4 numbers add up, so the Fork/join Framework fork the task to two subtasks, A subtask is responsible for computing 1+2, and subtask Two is responsible for computing 3+4, and then joins the results of two subtasks.

Because it is a result of the task, you must inherit Recursivetask, the implementation code is as follows:

001 Package FJ;
002
003 Import java.util.concurrent.ExecutionException;
004
10T Import Java.util.concurrent.ForkJoinPool;
006
007

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.