Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the principles of Fork and Join

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what the principles of Fork and Join are, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

I only heard the P8 boss ask slowly: what do you know about JDK concurrency tools?

I began to carefully comb through my years of experience in concurrent eight-part essays and said:

Concurrency tools such as thread pool, Future, CompletableFuture, and CompletionService help SE solve concurrency problems from a task point of view, rather than dwelling on the details of cooperation between threads, such as how to wait and notify between threads.

Simple parallel task

Thread pool + Future combination fist

There is an aggregation relationship between tasks.

Polymerization of AND and OR, one fresh trick of CompletableFuture

Batch parallel tasks

CompletionService is a shuttle.

Concurrent programming can be divided into three aspects: division of labor, cooperation and mutual exclusion.

When you focus on tasks, you will find that your perspective has popped out of the details of concurrent programming, while using real-world thinking patterns and analogies to real-world division of labor, thread pools, Future, CompletableFuture and CompletionService can all be classified as division of labor issues.

Realistic workflow flowchart of simple parallel tasks, aggregate tasks, and batch parallel tasks

These three task models basically cover the concurrent scenarios in daily work, but there is also a "divide-and-conquer" task model.

Divide and conquer, a way of thinking and mode of thinking to solve complex problems. Decompose a complex problem into several similar subproblems, and then decompose the subproblems into smaller subproblems, until the subproblems are so simple that they can be solved directly. Theoretically, solving every problem corresponds to a task, so the division and conquest of the problem is actually the division and conquest of the task.

P8 boss asked directly, what is the divide-and-conquer task model?

The divide and conquer task model can be divided into two stages:

Task decomposition

The task is divided into subtasks iteratively, and the result can be calculated until the subtasks. The specific affairs of the district shall be assigned to each local administrator.

Result merge

Merge the execution results of subtasks layer by layer until the final result is obtained. The local administrators finally report the results of governance to their superiors.

Just like the bureaucracy:

So how do you usually use Fork/Join for developers?

I, I didn't pass it usually, so I memorized it. Fortunately, I also prepared this question before the interview.

Fork/Join is a parallel computing framework to support the divide-and-conquer task model

Task decomposition in Fork corresponding divide-and-conquer Task Model

Join merging corresponding results

The Fork/Join computing framework mainly consists of two parts:

Thread pool ForkJoinPool for divide and conquer tasks

Divide and conquer task ForkJoinTask

The relationship between the two is similar to ThreadPoolExecutor and Runnable, where tasks are submitted to the thread pool, except that divide and conquer tasks have their own unique task type, ForkJoinTask.

ForkJoinTask

JDK7 provides an abstract class with the following core methods:

Fork ()

Execute a subtask asynchronously

Join ()

Block the current thread to wait for the execution result of the subtask

ForkJoinTask has two subclasses, RecursiveAction and RecursiveTask, both of which obviously use recursion to handle divide and conquer tasks. Both subclasses define the abstract method compute ():

RecursiveAction#compute () has no return value

RecursiveTask#compute () has a return value

Note that both classes are abstract classes, using the subclass implementation to be defined.

See P8 began to sneer, it seems to ask the source level principle!

Then tell me how Fork/Join works.

Fortunately, I know Ali interview routines, all java tools, must ask in-depth source code.

Because the core of Fork/Join is ForkJoinPool, let me explain the principle of ForkJoinPool in depth.

ThreadPoolExecutor is essentially a producer-consumer implementation with an internal task queue that acts as a communication medium between producers and consumers. ThreadPoolExecutor can have multiple worker threads, all of which share a task queue.

ForkJoinPool is essentially a producer-consumer implementation, but smarter.

ForkJoinPool working schematic diagram

There is only one task queue within ThreadPoolExecutor, while there are multiple task queues within ForkJoinPool. When you call ForkJoinPool#invoke () or submit () to submit a task, ForkJoinPool submits the task to a task queue through routing rules. If the task creates subtasks during execution, then the subtasks will be submitted to the task queue corresponding to the worker thread.

If the queue of tasks corresponding to the worker thread is empty, will there be no work to do?

NovelForkJoinPool has a "task theft" mechanism. If the worker thread is idle, it will "steal" tasks from other work task queues. For example, in the figure just now, thread T2 corresponding to the task queue is empty.

Then it will "steal" the tasks of the task queue corresponding to thread T1. In this way, all worker threads will not be idle.

ForkJoinPool's task queue uses a double-ended queue, and the worker thread normally acquires the task and "steals the task" from different sides of the task queue, which can also avoid a lot of unnecessary data competition.

ForkJoinPool supports the task theft mechanism, which can make the workload of all threads basically fair. Some threads are busy and some are always touching fish, so they have good performance and are a very fair leader.

Parallel flows in Java8's Stream API are also based on ForkJoinPool.

By default, all parallel flow calculations share a ForkJoinPool, and the default number of threads for this shared ForkJoinPool is the CPU core.

If all parallel flow computations are CPU-intensive, there is no problem at all, but if there is Imax O-intensive parallel flow computing, it is likely to slow down the performance of the whole system because of a very slow Imax O computing. Therefore, it is recommended to use different ForkJoinPool to perform different types of computing tasks.

About what Fork and Join principles are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report