In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the ForkJoin framework in Java concurrent programming? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. What is the ForkJoin framework
The ForkJoin framework is provided in java's JUC package to deal with some heavy tasks, which will be divided into multiple small tasks, and the results will be summarized to Result after the completion of multiple small tasks, reflecting the idea of "divide and conquer". The first step is to split the fork task and divide the large task into multiple small tasks; the second step, merge join, will merge the processing results of the small tasks into one result.
2 、 ForkJoinTask
ForkJoinTask is the task provided by the ForkJoin framework. API,ForkJoinTask is an abstract class with two main implementation classes, RecursiveTask and RecursiveAction. The main difference between RecursiveTask and RecursiveAction is that RecursiveAction does not return a value, while RecursiveTask has a return value.
3 、 ForkJoinPool
The ForkJoinPool class is the thread pool implementation of the forkjoin framework and is based on the ExecutorService interface. This thread pool is just joined by jdk1.7 and is used to manage threads and perform forkjoin tasks. For the use of thread pool, we use ThreadPoolExecutor more, you can look at the uml class diagram in idea, you can see that ForkJoinPool and ThreadPoolExecutor implementation are similar.
ForkJoinPool () ForkJoinPool (int parallelism) ForkJoinPool (int parallelism, ForkJoinWorkerThreadFactory factory, UncaughtExceptionHandler handler, boolean asyncMode)
Several important parameters:
Parallelism: parallelism, parallel execution thread, can be specified or not specified, if not specified, the available thread is created according to the number of cpu cores
ForkJoinWorkerThreadFactory: factory implementation for creating threads
UncaughtExceptionHandler: callback handling for unknown exception interrupts
AsyncMode: whether asynchronous. Default is false.
When you use it, you can create ForkJoinPool directly. If no parameters are passed or no parameters are passed, the number of parallel threads specified by default is Runtime.getRunTime () .availableProcessors ();, which indicates the number of available threads created according to the number of cpu cores.
ForkJoinPool forkJoinPool = new ForkJoinPool (); ArraySortTask task = new ArraySortTask (array, 0, size); forkJoinPool.submit (task); task.get ()
It is also available to pass parameters, specify the parallelism of public ForkJoinPool (int parallelism), parallelism parallelism, and execute several threads in parallel.
There are several ways to add a forkjoin task to the FrokJoinPool thread pool
Execute (): calls its fork method to split work between multiple threads.
Invoke (): call the invoke method on the ForkJoinPool thread pool
Submit (): a Future object is returned, which can be monitored by Future. The result is returned when the task is completed.
4. Print Fibonacci series
The ForkJoin framework can be used in some recursive scenarios, and you can be familiar with Fibonacci series, because it is sometimes asked in interviews that the characteristic of Fibonacci series is that the result of the last item is equal to the sum of the first two items.
Package com.example.concurrent.forkjoin;import java.util.concurrent.ExecutionException;import java.util.concurrent.ForkJoinPool;import java.util.concurrent.ForkJoinTask;import java.util.concurrent.RecursiveTask;/** Fibonacci series * *
* * @ author nicky.ma * modified record * modified version: modified by: modified date: 16:22 on 2021-10-12 modified content: * * / public class Fibonacci extends RecursiveTask {private int n; public Fibonacci (int n) {this.n = n;} @ Override protected Integer compute () {if (n > > 1) / / split task invokeAll (new ArraySortTask (array, lo, mid), new ArraySortTask (array, mid, hi)); / / merge result merge (lo, mid, hi);}} / / implementation details follow: static final int THRESHOLD = 1000 Void sortSequentially (int lo, int hi) {Arrays.sort (array, lo, hi);} void merge (int lo, int mid, int hi) {long [] buf = Arrays.copyOfRange (array, lo, mid); for (int I = 0, j = lo, k = mid; I < buf.length; jacks +) array [j] = (k = = hi | buf [I] < array [k])? Buf [iTunes +]: array [knot +];} public static void main (String [] args) throws ExecutionException, InterruptedException {int size = 10000; long [] array = new long [size]; Random random = new Random (); for (int I = 0; I < size; ionization +) {array [I] = random.nextInt ();} ForkJoinPool forkJoinPool = new ForkJoinPool () ArraySortTask task = new ArraySortTask (array, 0, size); forkJoinPool.submit (task); task.get (); for (long a: array) {System.out.println (a) } the answer to the question on how to understand the ForkJoin framework in Java concurrent programming is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.