使用Java7提供的Fork/Join框架

Java 分而治之,Fork/Join 框架实战 Java 分而治之,Fork/Join 框架 阅读详情

在Java7中,JDK提供对多线程开发提供了一个非常强大的框架,就是Fork/Join框架。这个是对原来的Executors更

进一步,在原来的基础上增加了并行分治计算中的一种Work-stealing策略,就是指的是。当一个线程正在等待他创建的

子线程运行的时候,当前线程如果完成了自己的任务后,就会寻找还没有被运行的任务并且运行他们,这样就是和

Executors这个方式最大的区别,更加有效的使用了线程的资源和功能。所以非常推荐使用Fork/Join框架。


下面我们以一个例子来说明这个框架如何使用,主要就是创建一个含有10000个资源的List,分别去修改他的内容。


[java]  view plain  copy
 print ?
  1. package com.bird.concursey.charpet8;  
  2.   
  3. /** 
  4.  * store the name and price of a product 
  5.  * @author bird 2014年10月7日 下午11:23:14 
  6.  */  
  7. public class Product {  
  8.   
  9.     private String name;  
  10.     private double price;  
  11.   
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.   
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.   
  20.     public double getPrice() {  
  21.         return price;  
  22.     }  
  23.   
  24.     public void setPrice(double price) {  
  25.         this.price = price;  
  26.     }  
  27.   
  28. }  


[java]  view plain  copy
 print ?
  1. package com.bird.concursey.charpet8;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. /** 
  7.  * generate a list of random products 
  8.  * @author bird 
  9.  * 2014年10月7日 下午11:24:47 
  10.  */  
  11. public class ProductListGenerator {  
  12.       
  13.     public List<Product> generate(int size) {  
  14.         List<Product> list = new ArrayList<Product>();  
  15.         for(int i = 0 ; i < size; i++) {  
  16.             Product product = new Product();  
  17.             product.setName("Product" + i);  
  18.             product.setPrice(10);  
  19.             list.add(product);  
  20.         }  
  21.         return list;  
  22.     }  
  23. }  


[java]  view plain  copy
 print ?
  1. package com.bird.concursey.charpet8;  
  2.   
  3. import java.util.List;  
  4. import java.util.concurrent.ForkJoinPool;  
  5. import java.util.concurrent.RecursiveAction;  
  6. import java.util.concurrent.TimeUnit;  
  7.   
  8. public class Task extends RecursiveAction {  
  9.   
  10.     private static final long serialVersionUID = 1L;  
  11.     // These attributes will determine the block of products this task has to  
  12.     // process.  
  13.     private List<Product> products;  
  14.     private int first;  
  15.     private int last;  
  16.     // store the increment of the price of the products  
  17.     private double increment;  
  18.   
  19.     public Task(List<Product> products, int first, int last, double increment) {  
  20.         super();  
  21.         this.products = products;  
  22.         this.first = first;  
  23.         this.last = last;  
  24.         this.increment = increment;  
  25.     }  
  26.   
  27.     /** 
  28.      * If the difference between the last and first attributes is greater than 
  29.      * or equal to 10, create two new Task objects, one to process the first 
  30.      * half of products and the other to process the second half and execute 
  31.      * them in ForkJoinPool using the invokeAll() method. 
  32.      */  
  33.     @Override  
  34.     protected void compute() {  
  35.         if (last - first < 10) {  
  36.             updatePrices();  
  37.         } else {  
  38.             int middle = (first + last) / 2;  
  39.             System.out.printf("Task: Pending tasks:%s\n", getQueuedTaskCount());  
  40.             Task t1 = new Task(products, first, middle + 1, increment);  
  41.             Task t2 = new Task(products, middle + 1, last, increment);  
  42.             invokeAll(t1, t2);  
  43.         }  
  44.     }  
  45.   
  46.     private void updatePrices() {  
  47.         for (int i = first; i < last; i++) {  
  48.             Product product = products.get(i);  
  49.             product.setPrice(product.getPrice() * (1 + increment));  
  50.         }  
  51.     }  
  52.   
  53.     public static void main(String[] args) {  
  54.         ProductListGenerator productListGenerator = new ProductListGenerator();  
  55.         List<Product> products = productListGenerator.generate(10000);  
  56.         Task task = new Task(products, 0, products.size(), 0.2);  
  57.   
  58.         ForkJoinPool pool = new ForkJoinPool();  
  59.         pool.execute(task);  
  60.   
  61.         do {  
  62.             System.out.printf("Main: Thread Count: %d\n",  
  63.                     pool.getActiveThreadCount());  
  64.             System.out.printf("Main: Thread Steal: %d\n", pool.getStealCount());  
  65.             System.out.printf("Main: Parallelism: %d\n", pool.getParallelism());  
  66.             try {  
  67.                 TimeUnit.MILLISECONDS.sleep(5);  
  68.             } catch (InterruptedException e) {  
  69.                 e.printStackTrace();  
  70.             }  
  71.         } while (!task.isDone());  
  72.           
  73.         pool.shutdown();  
  74.           
  75.         if(task.isCompletedNormally()) {  
  76.             System.out.printf("Main: The process has completed normally.\n");  
  77.         }  
  78.           
  79.         for(Product product : products) {  
  80.             if(product.getPrice() != 12) {  
  81.                 System.out.printf("Product %s: %f\n",product.getName(),product.getPrice());  
  82.             }  
  83.         }  
  84.           
  85.         System.out.println("Main: End of the program.\n");  
  86.     }  
  87.   
  88. }  


In this example, you have created a ForkJoinPool object and a subclass of the
ForkJoinTask class that you execute in the pool. To create the ForkJoinPool object,
you have used the constructor without arguments, so it will be executed with its default
configuration. It creates a pool with a number of threads equal to the number of processors
of the computer. When the ForkJoinPool object is created, those threads are created and
they wait in the pool until some tasks arrive for their execution.


Since the Task class doesn't return a result, it extends the RecursiveAction class. In the
recipe, you have used the recommended structure for the implementation of the task. If the
task has to update more than 10 products, it divides those set of elements into two blocks,
creates two tasks, and assigns a block to each task. You have used the first and last
attributes in the Task class to know the range of positions that this task has to update in the
list of products. You have used the first and last attributes to use only one copy of the
products list and not create different lists for each task.


To execute the subtasks that a task creates, it calls the invokeAll() method. This is a
synchronous call, and the task waits for the finalization of the subtasks before continuing
(potentially finishing) its execution. While the task is waiting for its subtasks, the worker thread
that was executing it takes another task that was waiting for execution and executes it. With
this behavior, the Fork/Join framework offers a more efficient task management than the
Runnable and Callable objects themselves.


The invokeAll() method of the ForkJoinTask class is one of the main differences
between the Executor and the Fork/Join framework. In the Executor framework, all the tasks
have to be sent to the executor, while in this case, the tasks include methods to execute and
control the tasks inside the pool. You have used the invokeAll() method in the Task class,
that extends the RecursiveAction class that extends the ForkJoinTask class.

You have sent a unique task to the pool to update all the list of products using the execute()
method. In this case, it's an asynchronous call, and the main thread continues its execution.
You have used some methods of the ForkJoinPool class to check the status and the
evolution of the tasks that are running. The class includes more methods that can be useful
for this purpose. See the Monitoring a Fork/Join pool recipe for a complete list of
those methods.

Finally, like with the Executor framework, you should finish ForkJoinPool using the
shutdown() method.
===========================================================================================================================================================================



ava7引入了Fork Join的概念,来更好的支持并行运算。顾名思义,Fork Join类似与流程语言的分支,合并的概念。也就是说Java7 SE原生支持了在一个主线程中开辟多个分支线程,并且根据分支线程的逻辑来等待(或者不等待)汇集,当然你也可以fork的某一个分支线程中再开辟Fork Join,这也就可以实现Fork Join的嵌套。

有两个核心类ForkJoinPool和ForkJoinTask。

ForkJoinPool实现了ExecutorService接口,起到线程池的作用。所以他的用法和Executor框架的使用时一样的,当然Fork Join本身就是Executor框架的扩展。ForkJoinPool有3个关键的方法,来启动线程,execute(...),invoke(...),submit(...)。具体描述如下:

 客户端非fork/join调用内部调用fork/join
异步执行execute(ForkJoinTask)ForkJoinTask.fork
等待获取结果invoke(ForkJoinTask)ForkJoinTask.invoke
执行,获取Futruesubmit(ForkJoinTask)ForkJoinTask.fork(ForkJoinTasks are Futures)

 

 ForkJoinTask是分支合并的执行任何,分支合并的业务逻辑使用者可以再继承了这个抽先类之后,在抽象方法exec()中实现。其中exec()的返回结果和ForkJoinPool的执行调用方(execute(...),invoke(...),submit(...)),共同决定着线程是否阻塞,具体请看下面的测试用例。

首先,用户需要创建一个自己的ForkJoinTask。代码如下:

Java代码   收藏代码
  1. public class MyForkJoinTask<V> extends ForkJoinTask<V> {  
  2.   
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = 1L;  
  7.       
  8.     private V value;  
  9.       
  10.     private boolean success = false;  
  11.       
  12.     @Override  
  13.     public V getRawResult() {  
  14.         return value;  
  15.     }  
  16.   
  17.     @Override  
  18.     protected void setRawResult(V value) {  
  19.         this.value = value;  
  20.     }  
  21.   
  22.     @Override  
  23.     protected boolean exec() {  
  24.         System.out.println("exec");  
  25.         return this.success;  
  26.     }  
  27.   
  28.     public boolean isSuccess() {  
  29.         return success;  
  30.     }  
  31.   
  32.     public void setSuccess(boolean isSuccess) {  
  33.         this.success = isSuccess;  
  34.     }  
  35.   
  36. }  

 

测试ForkJoinPool.invoke(...):

Java代码   收藏代码
  1. @Test  
  2.     public void testForkJoinInvoke() throws InterruptedException, ExecutionException {  
  3.         ForkJoinPool forkJoinPool = new ForkJoinPool();  
  4.         MyForkJoinTask<String> task = new MyForkJoinTask<String>();  
  5.         task.setSuccess(true);  
  6.         task.setRawResult("test");  
  7.         String invokeResult = forkJoinPool.invoke(task);  
  8.         assertEquals(invokeResult, "test");  
  9.     }  
  10.       
  11.     @Test  
  12.     public void testForkJoinInvoke2() throws InterruptedException, ExecutionException {  
  13.         final ForkJoinPool forkJoinPool = new ForkJoinPool();  
  14.         final MyForkJoinTask<String> task = new MyForkJoinTask<String>();  
  15.         new Thread(new Runnable() {  
  16.             public void run() {  
  17.                 try {  
  18.                     Thread.sleep(1000);  
  19.                 } catch (InterruptedException e) {  
  20.                 }  
  21.                   
  22.                 task.complete("test");  
  23.             }  
  24.         }).start();  
  25.           
  26.         // exec()返回值是false,此处阻塞,直到另一个线程调用了task.complete(...)  
  27.         String result = forkJoinPool.invoke(task);  
  28.         System.out.println(result);  
  29.     }  
  30.       
  31.     @Test  
  32.     public void testForkJoinSubmit() throws InterruptedException, ExecutionException {  
  33.         final ForkJoinPool forkJoinPool = new ForkJoinPool();  
  34.         final MyForkJoinTask<String> task = new MyForkJoinTask<String>();  
  35.           
  36.         task.setSuccess(true); // 是否在此任务运行完毕后结束阻塞  
  37.         ForkJoinTask<String> result = forkJoinPool.submit(task);  
  38.         result.get(); // 如果exec()返回值是false,在此处会阻塞,直到调用complete  
  39.     }  

 

测试ForkJoinPool.submit(...):

Java代码   收藏代码
  1. @Test  
  2.     public void testForkJoinSubmit() throws InterruptedException, ExecutionException {  
  3.         final ForkJoinPool forkJoinPool = new ForkJoinPool();  
  4.         final MyForkJoinTask<String> task = new MyForkJoinTask<String>();  
  5.           
  6.         task.setSuccess(true); // 是否在此任务运行完毕后结束阻塞  
  7.         ForkJoinTask<String> result = forkJoinPool.submit(task);  
  8.         result.get(); // 如果exec()返回值是false,在此处会阻塞,直到调用complete  
  9.     }  
  10.       
  11.     @Test  
  12.     public void testForkJoinSubmit2() throws InterruptedException, ExecutionException {  
  13.         final ForkJoinPool forkJoinPool = new ForkJoinPool();  
  14.         final MyForkJoinTask<String> task = new MyForkJoinTask<String>();  
  15.           
  16.         forkJoinPool.submit(task);  
  17.         Thread.sleep(1000);  
  18.     }  
  19.       
  20.     @Test  
  21.     public void testForkJoinSubmit3() throws InterruptedException, ExecutionException {  
  22.         final ForkJoinPool forkJoinPool = new ForkJoinPool();  
  23.         final MyForkJoinTask<String> task = new MyForkJoinTask<String>();  
  24.         new Thread(new Runnable() {  
  25.             public void run() {  
  26.                 try {  
  27.                     Thread.sleep(1000);  
  28.                 } catch (InterruptedException e) {  
  29.                 }  
  30.                   
  31.                 task.complete("test");  
  32.             }  
  33.         }).start();  
  34.           
  35.         ForkJoinTask<String> result = forkJoinPool.submit(task);  
  36.         // exec()返回值是false,此处阻塞,直到另一个线程调用了task.complete(...)  
  37.         result.get();   
  38.         Thread.sleep(1000);  
  39.     }  

 

测试ForkJoinPool.execute(...):

Java代码   收藏代码
  1. @Test  
  2.    public void testForkJoinExecute() throws InterruptedException, ExecutionException {  
  3.        ForkJoinPool forkJoinPool = new ForkJoinPool();  
  4.        MyForkJoinTask<String> task = new MyForkJoinTask<String>();  
  5.        forkJoinPool.execute(task); // 异步执行,无视task.exec()返回值。  
  6.    }  

 


智能驾驶小白必看:MEB低速紧急制动系统的工作原理与传感器配置详解 本文深入解析了MEB低速紧急制动系统的工作原理与传感器配置。该系统作为智能驾驶的关键安全冗余,专为应对停车场、拥堵路段等复杂低速场景设计。文章详细拆解了其如何通过超声波雷达、环视摄像头等传感器融合感知环境,并基于碰撞时间(TTC)等模型进行风险评估与分级制动,最终实现主动防碰撞,是智能驾驶技术实用化的重要体现。 阅读详情

相关推荐

Autodesk Genuine Service卸载失败?试试这个隐藏文件删除法(Win8-Win11通用)

本文针对Autodesk Genuine Service在Windows系统(Win8-Win11)中卸载失败的常见难题,提供了深度解析与根治方案。核心方法是手动定位并删除两个关键的隐藏文件(ProductInformation.pit和id.dat),以解除文件锁定或数据损坏导致的卸载障碍,随后再通过标准流程即可成功卸载。该方法通用性强,能有效解决这一困扰设计师和工程师的顽固问题。

weixin_29220405的博客 1477

java中的fork、join是什么?这个fork和我们常见到的如“fork一个进程去执行xxx”是一回事么?

操作系统中的fork和java中的fork是不是一回事? java中的fork/join框架是什么?怎么用? 工作窃取算法(work-stealing)是什么?

github_38727595的博客 1665

ArcGIS基于DEM提取微流域单元(附练习数据下载)

流域作为一个相对独立的地理单元,与行政区的划分有着本质上的区别。流域的划分更注重自然因素,流域作为一个综合的自然整体,在其内部形成了一个天然的物质与能量流动的场所,并通过水流的作用,与周围的区域进行物质和能量的交换。流域的精确划分作为一个研究的基础工作,那么接下来给大家说一下利用DEM提取小流域的案例教程——以贵州省遵义市正安县为例。 1 数据获取 地理空间数据云(http://www.gscloud.cn/),数据分辨率为30m。也可直接点击这篇直接下载→全国30m90mDEM下载。 若想要获得更高精度.

qq_43173805的博客 4837

Java基础——一文读懂Fork/Join框架

如图一个规模为T的任务划分成12个子任务,分别有4个线程在执行。如果当前线程没有可执行任务时,会从其他线程的任务队列中窃取任务来执行。工作窃取算法保证了cpu不会处于空闲状态。fork/Join框架的思想是将一个规模为n的大任务,fork成几个规模较小的K个子任务,最后合并所有子任务的结果,返回最终结果。由此我们可以看出,Java8的Stream并行流底层代码,确实采用了Fork/Join框架来实现。如下所示代码,利用Java8的并行流,对1到5的数进行相乘。接口TerminalOp。

mathGaosi的博客 626

深入了解 Google Gemini:主要功能是什么以及我们为什么要使用它?

Google Gemini开启AI新纪元,以其独特的多模式功能重塑技术未来。

KompasAI的博客 4070

Warning-[INFL_DELTA] Too many events zero delay loop

项目场景: 运用VCS工具执行仿真的时候,遇到如下错误提示,但仿真还在继续。 Warning-[INFL_DELTA] Too many events Possible zero delay loop. Too many events in the simulation at time 0 fs. 问题描述: 为什么会有这个问题; 什么导致了这个问题; 原因分析: 1、为什么会有这个问题 由于竞争条件或编码风格不明确,可能会产生零延时回路,导致仿真挂起或仿真时间无限。 2、什么导致了这个问题 举

hh199203的博客 1万+

java fork菜鸟_fork()函数的简单例子

1.fork()函数fork()是一个系统调用,用于创建进程。创建的这个进程与原来进程几乎完全相同。这个新产生的进程称为子进程。一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间。然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同。相当于克隆了一个自己。需要注意的一点:就是调用fork之后,两个进程同时执行的代码段是fork函数之后的代码,...

weixin_30855927的博客 3648

基于Python大数据的智能交通分析系统

该系统能够实时监测交通状况,分析交通流量,预测交通趋势,为交通管理部门提供决策支持,帮助缓解交通拥堵,提高交通运行效率,减少交通事故发生率,改善市民出行体验。它能够为交通管理部门提供决策支持,帮助缓解交通拥堵,提高交通运行效率,减少交通事故发生率,改善市民出行体验,促进交通产业的健康发展。数据采集层:通过传感器、GPS设备、交通摄像头等数据源,实时采集交通数据,如车辆速度、位置、道路拥堵情况等。数据分析层:运用统计学方法、机器学习算法等,对处理后的数据进行深入分析,挖掘交通规律,预测交通趋势。

qq3828614760的博客 2969

NYOJ 164 Game of Connections

 Game of Connections 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in c

zlj ☼☼☼的博客 2万+

YOLOv8融合HAttention:激活更多像素的注意力机制科研实践指南

HAttention通过层级化像素激活与融合的设计,为YOLOv8的像素级特征利用能力带来了质的飞跃。从原理理解到代码实现,再到集成调优,每一步都是科研中“解决像素信息遗漏难题”的具体实践。如果你在实验中发现某类细节或大尺度目标的检测仍有不足,可以尝试结合数据增强(如随机裁剪、多尺度训练)来强化模型对像素特征的学习能力。科研的价值在于不断挖掘技术的潜力,希望这份指南能真正帮你在目标检测的像素级特征利用领域找到新的突破点,祝你在科研探索中持续收获创新与成长。

FJN110的博客 106

使用自己的数据跑通mmsegmentation

使用自己的数据跑通mmsegmentation,解决以下报错:1)type object 'FileClient' has no attribute 'infer_client' 2)It is expected output_size equals to 2, but got size 3 3)训练测试结果报NAN 4)KeyError: 'MyDataset is not in the dataset registry' 5)预训练模型和加载的网络结构层名有差异,unexpected key .....

猫猫与橙子的博客 1657

redis 3.0的集群部署

文章转载自:http://hot66hot.iteye.com/blog/2050676 首页 资讯 精华 论坛 问答 博客 专栏 群组 更多 ▼ 您还未登录 ! 登录 注册 zhangyijun 博客 微博 相册 收藏 留言 关于我   redis-cluster研究和使用

myrainblues的专栏 22万+

FX5U PLC运动控制库安装(PLCopen Motion Control FB参考)

下面我们介绍三菱FX5U PLC的运动控制库PLCopen Motion Control FB的安装。

RXXW_Dor的博客 1534

java forkjoin MySQL_使用Java7提供Fork/Join框架

在Java7在。JDK它提供了多线程开发提供了一个非常强大的框架。这是Fork/Join框架。这是原来的Executors更多进一步,在原来的基础上添加了并行分治计算中的一种Work-stealing策略。就是指的是。当一个线程正在等待他创建的子线程执行的时候,当前线程假设完毕了自己的任务后,就会寻找还没有被执行的任务而且执行他们,这样就是和Executors这个方式最大的差别,更加有效的使用了线...

weixin_35260789的博客 128

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Java7提供的Fork/Join框架实现高并发程序,你会使用吗?

Fork/Join框架位于J.U.C(java.util.concurrent)中,是Java7中提供的用于执行并行任务的框架,其可以将大任务分割成若干个小任务,最终汇总每个小任务的结果后得到最终结果。

华为云官方博客 995

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

使用Java7提供Fork/Join框架

在Java7在。JDK它提供了多线程开发提供了一个非常强大的框架。这是Fork/Join框架。这是原来的Executors更多 进一步,在原来的基础上添加了并行分治计算中的一种Work-stealing策略。就是指的是。当一个线程正在等待他创建的 子线程执行的时候,当前线程假设完毕了自己的任务后,就会寻找还没有被执行的任务而且执行他们,这样就是和 Executors这个方式...

weixin_30492047的博客 80

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

高并发之——如何使用Java7提供的Fork/Join框架实现高并发程序?

Fork/Join框架 位于J.U.C(java.util.concurrent)中,是Java7中提供的用于执行并行任务的框架,其可以将大任务分割成若干个小任务,最终汇总每个小任务的结果后得到最终结果。基本思想和Hadoop的MapReduce思想类似。 主要采用的是工作窃取算法(某个线程从其他队列里窃取任务来执行),并行分治计算中的一种Work-stealing策略 为什么需要使用工作窃取...

冰河的专栏 1311

jeesit框架如何使用_如何使用Java7中提供的Fork/Join框架实现高并发程序?

写在前面在JDK中,提供了这样一种功能:它能够将复杂的逻辑拆分成一个个简单的逻辑来并行执行,待每个并行执行的逻辑执行完成后,再将各个结果进行汇总,得出最终的结果数据。有点像Hadoop中的MapReduce。ForkJoin是由JDK1.7之后提供的多线程并发处理框架。ForkJoin框架的基本思想是分而治之。什么是分而治之?分而治之就是将一个复杂的计算,按照设定的阈值分解成多个计算,然后将各个计...

weixin_39814093的博客 229

Fork/Join - 并发框架源码解析

什么是Fork/Join框架 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架。 我们再通过Fork和Join这两个单词来理解下Fork/Join框架,Fork就是把一个大任务切分为若干子任务并行的执行,Join就是合并这些子任务的执行结果,最后得到这个大任务的结果。比如计算1+2+ … +100...

.... 永远年轻,永远热泪盈眶 5万+

什么是 Fork/Join 框架?Java 中如何使用 Fork/Join 框架?

Fork/Join 框架中的任务类需要继承自或类。其中,类用于有返回值的任务,类用于没有返回值的任务。任务类需要实现compute()方法,该方法是具体的计算逻辑。this.n = n;在上面的代码中,类继承自类,compute()方法实现了斐波那契数列的计算逻辑。如果n的值小于等于 1,则直接返回n。否则,创建两个新的任务f1和f2,分别计算n - 1和n - 2的斐波那契数列,然后调用f##fork()

程序员徐师兄的博客 389

Fork/Join框架介绍

http://blog.youkuaiyun.com/lubeijing2008xu/article/details/18036931#t5

八荒六合唯我独尊 611

JDK中Fork和Join框架

1.Fork和Join简介 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架。 Fork:把一个大任务分成若干个子任务进行执行,设置一个阀值,判断任务数量,如果超越阀值,无线分...

285
上一篇: jackson annotations注解详解
下一篇: 4种方法让SpringMVC接收多个对象
zljjava
博客等级 码龄15年 155粉丝 15原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值