Introduction to the Fork/join framework

Source: Internet
Author: User
Tags execution join thread

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 sum of 1000 numbers, 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:

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.

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.