In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Java JUC multithreaded Fork Join Pool". In daily operation, I believe many people have doubts about how to use Java JUC multithreaded Fork Join Pool. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use Java JUC multithreaded Fork Join Pool". Next, please follow the editor to study!
In the course of our work, when we are calculating a large value plus a large value and so on, the computer consumes a lot of CPU in serial calculation, and the efficiency is also very low.
At this time, we can choose multi-threaded branch merging to use multiple threads to calculate, and then merge the final results of each thread.
ForkJoinPool branch / merge framework work theft
Fork/Join framework: when necessary, a large task is split (fork) into several small tasks (when it can no longer be detached), and then the results of each small task operation are summarized in join.
As shown in the figure:
Differences between Fork/Join Framework and Thread Pool
uses the "work theft" mode (work-stealing): when a new task is executed, it can split it into smaller tasks and add small tasks to the thread queue, then steal one from a random thread queue and put it in its own queue.
Compared with the general thread pool implementation, the advantage of framework lies in the way it handles the tasks contained in it. In a normal thread pool, a thread is in a waiting state if the task it is performing cannot continue to run for some reason. In the fork/join framework implementation, if a sub-problem cannot continue to run because it is waiting for another sub-problem to complete. Then the thread dealing with the sub-problem will actively look for other unrun sub-problems to execute. This approach reduces the waiting time of threads and improves performance.
Without saying much, look at the code:
Package com.atxiaodei.javajuc.jucdemo;import org.junit.Test;import java.time.Duration;import java.time.Instant;import java.util.concurrent.ForkJoinPool;import java.util.concurrent.ForkJoinTask;import java.util.concurrent.RecursiveTask;import java.util.stream.LongStream / * * @ author wangmeng * @ date 2020-9-23 * ForkJoinPool branch / merge framework work theft * Preface: when we are calculating a large value plus a large value, etc., the computer consumes CPU in serial computing, and the efficiency is very low. At this time, we can choose * multithreaded branches to merge and use multiple threads to calculate. Then merge the results of each thread into the final result * * / public class TestForkJoinPool thread branches merge {public static void main (String [] args) {/ / 1.8 feature to get the calculation Instant start = Instant.now () ForkJoinPool pool = new ForkJoinPool (); ForkJoinTask task = new ForkJoinSumCalculate (0L, 500000000L); Long sum = pool.invoke (task); System.out.println (sum); Instant end = Instant.now (); System.out.println ("time spent:" + Duration.between (start, end). ToMillis ()) 1996-10590} @ Test public void test1 () {Instant start = Instant.now (); long sum = 0L; for (long I = 0L; I
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.