In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the Fork/Join framework. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The fork/join framework is a concrete implementation of the ExecutorService interface, which distributes tasks to worker threads in the thread pool, making better use of the benefits of multiprocessors and providing program performance. It is designed for types of work that can be recursively broken down into subtasks.
The fork/join framework is unique in that it uses work theft (work-stealing) algorithms. Idle worker threads that finish their work can steal tasks waiting to be executed from other work threads that are still in a busy state.
At the core of the fork/join framework is the ForkJoinPool class, which is an extension of the AbstractExecutorService class. ForkJoinPool implements the work theft algorithm and can perform ForkJoinTask tasks.
The first step in using the fork/join framework is to write code that performs part of the work, and similar pseudocode is as follows:
If (the current task is small enough) finish the task directly else divides the current task into two parts call the two parts and wait for the result
This generation is wrapped in a subclass of ForkJoinTask. However, it is usually RecursiveTask (which returns a result), or RecursiveAction.
Fork decomposes the new task, and join aggregates the results of the task. The general process is as follows:
Fork/Join actual use case
The core idea of fork/join is to divide and conquer, dividing a big task into one small task. The use of fork/join requires defining a task class to implement RecursiveTask or RecursiveAction, overriding the compute () method, defining the task split logic in the compute () method, and then submitting the task for execution with the help of ForkJoinPool. The fork/join framework will split the task according to the split logic defined in the compute () method. If there is a return value, you can get the return value with ForkJoinTask. Suppose there are many network requests that need to be executed concurrently, and then summarize the results. The code using fork/join is as follows:
Public class ForkJoinTest {/ / Test data static ArrayList urls = new ArrayList () {{add ("http://www.sina.com"); add (" http://www.baidu.com"); add ("http://www.sina.com"); add (" http://www.baidu.com"); add ("http://www.sina.com");)" Add ("http://www.baidu.com"); add (" http://www.sina.com"); add ("http://www.baidu.com"); add (" http://www.sina.com"); add ("http://www.baidu.com"); add (" http://www.sina.com");) " Add ("http://www.baidu.com"); add (" http://www.sina.com"); add ("http://www.baidu.com"); add (" http://www.sina.com"); add ("http://www.baidu.com"); add (" http://www.sina.com");) " Add ("http://www.baidu.com"); add (" http://www.sina.com"); add ("http://www.baidu.com"); add (" http://www.sina.com"); add ("http://www.baidu.com"); add (" http://www.sina.com");}}) " / / Core ForkJoinPool static ForkJoinPool firkJoinPool = new ForkJoinPool (3, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); public static void main (String args []) throws ExecutionException, InterruptedException {Job job = new Job (urls, 0, urls.size ()); ForkJoinTask forkJoinTask = firkJoinPool.submit (job); String result = forkJoinTask.get (); System.out.println (result);} public static String doRequest (String url) {/ / simulate network request return "Kody. Test... "+ url +"\ n ";} / / Custom task static class Job extends RecursiveTask {List urls; int start; int end; public Job (List urls, int start, int end) {this.urls = urls; this.start = start; this.end = end } / * * core task calculation, task split logic implementation * / @ Override protected String compute () {/ / calculate the size of the task int count = end-start; / / if the task split is small enough, execute the task if (count
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.