In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the analysis of the use of ForkJoin in java J.U.C, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
ForkJoin framework is a framework provided by Java7 for parallel execution of tasks. It is a framework that divides large tasks into several small tasks, and finally summarizes the results of each small task to get the results of large tasks.
Very similar to the idea of MapReduce. Literally, Fork is to cut a large task into several sub-tasks to execute in parallel, and Join is to merge the execution results of these sub-tasks and finally get the results of large tasks. Work theft algorithm is mainly used.
The work theft algorithm means that a thread steals a task from another queue to execute. The following is a flowchart of job theft:
Why use the job theft algorithm?
To join us, we need to do a relatively large task. We can divide this task into several independent subtasks. In order to reduce the competition between threads, we put these subtasks into different queues. Create a separate thread for each queue to execute the tasks in the queue. Threads correspond to the queue one by one. For example, A thread is responsible for handling the tasks in queue A, but some threads will finish the tasks in their own queue first, while other threads have corresponding tasks waiting to be processed, and the finished thread will help other threads do the work. so they steal a task from the queue of other threads to execute, and then they access the same queue. Therefore, in order to reduce the competition between the stolen task thread and the stolen task thread, the double-ended queue is usually used. the thread of the stolen task always takes the task from the head of the double-ended queue to execute, while the thread that steals the task always takes the task from the tail of the double-ended queue to execute. This advantage is to make full use of threads for parallel computing, reducing the competition between threads, and the disadvantage is that there is still competition in some cases (for example, when there is only one task in the double-ended queue). It also consumes more system resources (such as creating multiple threads and multiple double-ended queues).
For the ForkJoin framework, when a task is waiting for the end of a subtask created by him using the ForkJoin operation, the worker thread that executes the task looks for other unexecuted tasks and starts his execution. In this way, the thread makes full use of its running time to improve the performance of the application. In order to achieve this goal, the tasks performed by the ForkJoin framework have some limitations.
Limitation
Tasks can only use Fork and Join operations as synchronization mechanisms, and if other synchronization mechanisms are used, their worker threads will not be able to perform other tasks during synchronization. For example, if you enter a task into sleep in the forkjoin framework, the worker thread that is performing the task during sleep will not perform other tasks.
The task we split should not perform the IO operation. Such as reading and writing data files
Task cannot throw a check exception. They must be dealt with through the necessary code
At the core of the ForkJoin framework are two classes: ForkJoinPool and ForkJoinTask. ForkJoinPool is responsible for doing the implementation (including work theft algorithms). He manages the worker thread to provide the status of the task and their execution information. ForkJoinTask, on the other hand, mainly provides the mechanism to perform Fork and Join operations in a task.
@ Slf4jpublic class ForkJoinTaskExample extends RecursiveTask {public static final int threshold = 2; private int start; private int end; public ForkJoinTaskExample (int start, int end) {this.start = start; this.end = end;} @ Override protected Integer compute () {int sum = 0; / / calculate task boolean canCompute = (end-start) if the task is small enough
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.