In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the Fork or Join mode in JDK 7 is. It is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Jieshao
As multi-core chips gradually become the mainstream, most software developers inevitably need to know the knowledge of parallel programming. At the same time, mainstream programming languages are merging more and more parallel features into standard libraries or languages themselves. We can see that JDK is also ahead of the trend in this respect. In JDK Standard Edition 5, the parallel framework provided by Doug Lea has become part of the standard library (JSR-166). Subsequently, in JDK 6, some new parallelism features, such as the parallel collection framework, were incorporated into the standard library (JSR-166x). To this day, although Java SE 7 has not been officially released, some new features related to parallelism have emerged in JSR-166y:
1.Fork/Join mode
2.TransferQueue, which inherits from BlockingQueue and blocks "producers" when the queue is full
3.ArrayTasks/ListTasks, a class used to perform some array / list related tasks in parallel
4.IntTasks/LongTasks/DoubleTasks, a tool class for parallel processing of arrays of numeric types, providing functions such as sorting, finding, summing, minimizing, maximizing, etc.
Among them, support for the Fork/Join pattern is probably the most common new feature for developing parallel software. In JSR-166y, the Fork/Join pattern is widely used when Doug Lea implements ArrayTasks/ListTasks/IntTasks/LongTasks/DoubleTasks. Readers should also note that because JDK 7 has not yet been officially released, the features covered in this article may not be the same as the release.
The Fork/Join model has its own scope of application. If an application can be decomposed into multiple subtasks, and the final answer can be obtained by combining the results of multiple subtasks, then the application is suitable to be solved by Fork/Join mode. Figure 1 shows a schematic diagram of the Fork/Join mode. The Task at the top of the figure depends on the execution of the Task below, and only when all the subtasks are completed can the caller get the return result of Task 0.
Figure 1. Schematic diagram of Fork/Join mode
It can be said that the Fork/Join pattern can solve many kinds of parallel problems. By using the Fork/Join framework provided by Doug Lea, software developers only need to pay attention to the division of tasks and the combination of intermediate results to make full use of the excellent performance of the parallel platform. Other difficult problems related to parallelism, such as load balancing, synchronization, etc., can be solved by the framework in a unified way. In this way, we can easily get the benefits of parallelism and avoid the difficulty and error-prone disadvantage of parallel programming.
Use Fork/Join mode
Before we can try Fork/Join mode, we need to download the source code of JSR-166y from the Concurrency JSR-166 Interest Site hosted by Doug Lea, and we also need to install the latest version of JDK 6 (see Resources for the download URL). The way Fork/Join mode is used is very straightforward. First, we need to write a ForkJoinTask to complete the division of subtasks, the merging of intermediate results, and so on. Then, we give the ForkJoinTask to ForkJoinPool to complete the execution of the application.
Usually we don't inherit ForkJoinTask directly, it contains too many abstract methods. For a specific problem, we can choose different subclasses of ForkJoinTask to complete the task. RecursiveAction is a subclass of ForkJoinTask and represents the simplest type of ForkJoinTask: no return value is required, and there is no need to combine intermediate results after the subtasks have been executed. If we inherit from RecursiveAction, then we only need to overload the protected void compute () method. Next, let's look at how to create a subclass of ForkJoinTask for the quick sort algorithm:
Listing 1. Subclasses of ForkJoinTask
ClassSortTaskextendsRecursiveAction {finallong [] array; finalintlo; finalinthi; privateintTHRESHOLD=30; publicSortTask (long [] array) {this.array=array; this.lo=0; this.hi=array.length-1;} publicSortTask (long [] array,intlo,inthi) {this.array=array; this.lo=lo; this.hi=hi;} protectedvoidcompute () {if (hi-lo)
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.