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

How to analyze the principle of fork/join Framework in the New Features of JDK7

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

Share

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

How to analyze the principle of fork/join framework in the new features of JDK7? I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Principle analysis: fork decomposition, join combination. The essence of this framework is to decompose a task into multiple subtasks, each of which is processed by a separate thread. Recursive ideas are used here. The structure diagram of the frame can be referenced.

It is easy to use the fork/join framework

1. A general algorithm for solving subproblems

two。 How to decompose the problem

3. Inherit RecursiveAction and implement the compute () method

Pseudo code code

Result solve (Problem problem) {if (problem is small) directly solve problem else {split problem into independent parts fork new subtasks to solve each part join all subtasks compose result from subresults}

Here I explain the use of fork/join through an improved binary search. (later, it was found that choosing this case is a very failure, because the time for binary search is logn, and the cost of creating a thread is higher, which does not reflect the advantage of multithreaded binary search, so this code is not practical, just to illustrate how to use the framework:)

The code is as follows:

BinarySearchProblem.java

Java code

Package testjdk7; import java.util.Arrays; / * * @ author kencs@foxmail.com * / public class BinarySearchProblem {private final int [] numbers; private final int start; private final int end; public final int size; public BinarySearchProblem (int [] numbers,int start,int end) {this.numbers = numbers; this.start = start This.end = end; this.size = end-start;} public int searchSequentially (int numberToSearch) {/ / lazy, not writing binary search return Arrays.binarySearch (numbers,start, end, numberToSearch);} public BinarySearchProblem subProblem (int subStart,int subEnd) {return new BinarySearchProblem (numbers,start+subStart,start+subEnd) }}

BiSearchWithForkJoin.java

Java code

Package testjdk7; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; / * * @ author kencs@foxmail.com * / public class BiSearchWithForkJoin extends RecursiveAction {private final int threshold; private final BinarySearchProblem problem; public int result; private final int numberToSearch; public BiSearchWithForkJoin (BinarySearchProblem problem,int threshold,int numberToSearch) {this.problem = problem This.threshold = threshold; this.numberToSearch = numberToSearch;} @ Override protected void compute () {if (problem.size < threshold) {/ / less than the threshold, look for result = problem.searchSequentially (numberToSearch) directly with ordinary binary. } else {/ / decompose subtask int midPoint = problem.size/2; BiSearchWithForkJoin left = new BiSearchWithForkJoin (problem.subProblem (0, midPoint), threshold,numberToSearch); BiSearchWithForkJoin right = new BiSearchWithForkJoin (problem.subProblem (midPoint+1, problem.size), threshold,numberToSearch); invokeAll (left,right) Result = Math.max (left.result, right.result);}} / / Construction data private static final int [] data = new int [100000000]; static {for (int I = 0 int 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.

Share To

Development

Wechat

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

12
Report