Java对多线程~~~Fork/Join同步和异步帧
于Fork/Join骨架,当提交的任务,有两个同步和异步模式。它已被用于invokeAll()该方法是同步的。是任何
务提交后,这种方法不会返回直到全部的任务都处理完了。而还有还有一种方式,就是使用fork方法,这个是异步的。也
就是你提交任务后,fork方法马上返回。能够继续以下的任务。
这个线程也会继续执行。
以下我们以一个查询磁盘的以log结尾的文件的程序样例来说明异步的使用方法。
package com.bird.concursey.charpet8; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask; public class FolderProcessor extends RecursiveTask<List<String>> { private static final long serialVersionUID = 1L; private String path;
private String extension; public FolderProcessor(String path, String extension) {
super();
this.path = path;
this.extension = extension;
} @Override
protected List<String> compute() {
List<String> list = new ArrayList<String>();
List<FolderProcessor> tasks = new ArrayList<FolderProcessor>();
File file = new File(path);
File content[] = file.listFiles();
if(content != null) {
for(int i = 0; i < content.length; i++) {
if(content[i].isDirectory()) {
FolderProcessor task = new FolderProcessor(content[i].getAbsolutePath(), extension);
//异步方式提交任务
task.fork();
tasks.add(task);
}else{
if(checkFile(content[i].getName())) {
list.add(content[i].getAbsolutePath());
}
}
}
}
if(tasks.size() > 50) {
System.out.printf("%s: %d tasks ran.\n",file.getAbsolutePath(),tasks.size());
} addResultsFromTasks(list,tasks);
return list;
} /**
* that will add to the list of files
the results returned by the subtasks launched by this task.
* @param list
* @param tasks
*/
private void addResultsFromTasks(List<String> list,
List<FolderProcessor> tasks) {
for(FolderProcessor item: tasks) {
list.addAll(item.join());
}
} /**
* This method compares if the name of a file
passed as a parameter ends with the extension you are looking for
* @param name
* @return
*/
private boolean checkFile(String name) {
return name.endsWith(extension);
} public static void main(String[] args) {
ForkJoinPool pool = new ForkJoinPool();
FolderProcessor system = new FolderProcessor("C:\\Windows", "log");
FolderProcessor apps = new FolderProcessor("C:\\Program Files", "log"); pool.execute(system);
pool.execute(apps); pool.shutdown(); List<String> results = null;
results = system.join();
System.out.printf("System: %d files found.\n",results.size()); results = apps.join();
System.out.printf("Apps: %d files found.\n",results.size()); }
}
The key of this example is in the FolderProcessor class. Each task processes the content
of a folder. As you know, this content has the following two kinds of elements:
ff Files
ff Other folders
If the task finds a folder, it creates another Task object to process that folder and sends it to
the pool using the fork() method. This method sends the task to the pool that will execute it
if it has a free worker-thread or it can create a new one. The method returns immediately, so
the task can continue processing the content of the folder. For every file, a task compares its
extension with the one it's looking for and, if they are equal, adds the name of the file to the
list of results.
Once the task has processed all the content of the assigned folder, it waits for the finalization
of all the tasks it sent to the pool using the join() method. This method called in a task
waits for the finalization of its execution and returns the value returned by the compute()
method. The task groups the results of all the tasks it sent with its own results and returns
that list as a return value of the compute() method.
The ForkJoinPool class also allows the execution of tasks in an asynchronous way. You
have used the execute() method to send the three initial tasks to the pool. In the Main
class, you also finished the pool using the shutdown() method and wrote information about
the status and the evolution of the tasks that are running in it. The ForkJoinPool class
includes more methods that can be useful for this purpose. See the Monitoring a Fork/Join
pool recipe to see a complete list of those methods.
版权声明:本文博主原创文章,博客,未经同意不得转载。
Java对多线程~~~Fork/Join同步和异步帧的更多相关文章
- Java:多线程,线程同步,同步锁(Lock)的使用(ReentrantLock、ReentrantReadWriteLock)
关于线程的同步,可以使用synchronized关键字,或者是使用JDK 5中提供的java.util.concurrent.lock包中的Lock对象.本文探讨Lock对象. synchronize ...
- Java 并发编程 -- Fork/Join 框架
概述 Fork/Join 框架是 Java7 提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.下图是网上流传的 Fork Join 的 ...
- java 中的fork join框架
文章目录 ForkJoinPool ForkJoinWorkerThread ForkJoinTask 在ForkJoinPool中提交Task java 中的fork join框架 fork joi ...
- JUC组件扩展(二)-JAVA并行框架Fork/Join(二):同步和异步
在Fork/Join框架中,提交任务的时候,有同步和异步两种方式. invokeAll()的方法是同步的,也就是任务提交后,这个方法不会返回直到所有的任务都处理完了. fork方法是异步的.也就是你提 ...
- Java并发编程--Fork/Join框架使用
上篇博客我们介绍了通过CyclicBarrier使线程同步,可是上述方法存在一个问题,那就是假设一个大任务跑了2个线程去完毕.假设线程2耗时比线程1多2倍.线程1完毕后必须等待线程2完毕.等待的过程线 ...
- JUC组件扩展(二)-JAVA并行框架Fork/Join(四):监控Fork/Join池
Fork/Join 框架是为了解决可以使用 divide 和 conquer 技术,使用 fork() 和 join() 操作把任务分成小块的问题而设计的.主要实现这个行为的是 ForkJoinPoo ...
- JUC组件扩展(二)-JAVA并行框架Fork/Join(一):简介和代码示例
一.背景 虽然目前处理器核心数已经发展到很大数目,但是按任务并发处理并不能完全充分的利用处理器资源,因为一般的应用程序没有那么多的并发处理任务.基于这种现状,考虑把一个任务拆分成多个单元,每个单元分别 ...
- Java 并发之 Fork/Join 框架
什么是 Fork/Join 框架 Fork/Join 框架是一种在 JDk 7 引入的线程池,用于并行执行把一个大任务拆成多个小任务并行执行,最终汇总每个小任务结果得到大任务结果的特殊任务.通过其命名 ...
- Java并行任务框架Fork/Join
Fork/Join是什么? Fork意思是分叉,Join为合并.Fork/Join是一个将任务分割并行运行,然后将最终结果合并成为大任务的结果的框架,父任务可以分割成若干个子任务,子任务可以继续分割, ...
随机推荐
- [LeetCode145]Binary Tree Postorder Traversal
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- App设计相关网站
http://sketch.im/ 设计素材 principle 动效软件 http://principleformac.com/ 官网 http://principlecn.com/ 中文网
- 【Android进阶】自定义控件实现底部扇形展开菜单效果
这个项目是优化的其他人的,主要优化了界面菜单的显示,下面开始. 先看效果图 项目的总结构 下面开始贴代码,由于必要的地方都添加了注释,所以不过多讲解 anim_button.xml <?xml ...
- 【C语言探索之旅】 第二部分第五课:预处理
内容简介 1.课程大纲 2.第二部分第五课: 预处理 3.第二部分第六课预告: 创建你自己的变量类型 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语 ...
- Oracle 重建索引脚本
该指数是一个有力的武器,以提高数据库的查询性能. 没有索引,喜欢同样的标签库没有书籍,找书,他们想预订比登天还难.中,尤其是在批量的DML的情形下会产生对应的碎片.以及B树高度会发生对应变化.因此能够 ...
- 接近带给你AngularJS - 经验说明示例
接近带给你AngularJS列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自己定义指令 ------------ ...
- 介绍一款替代SSMS的sqlserver管理工具 toad for sqlserver5.7
原文:介绍一款替代SSMS的sqlserver管理工具 toad for sqlserver5.7 toad for sqlserver5.7 虽然SSMS很好很强大,不过有时候使用一些第三方工具可以 ...
- Nancy 框架学习
Nancy 框架 1.是一个轻量级用于构建http相应的web框架: 2.与mvc类似,有自己的路由机制: 3.可以处理 DELETE , GET , HEAD , OPTIONS , POS ...
- Google免费的SVN服务器管理VS2010代码
原文:Google免费的SVN服务器管理VS2010代码 前言 Google免费为我们提供了代码管理的SVN服务器.首先我这里用的Win7 64的电脑系统,用VS2010进行的代码开发.这里管理代码需 ...
- rest-work-eat-study-rest-work-eat or rest-rest-work-work-eat-eat..
words are for your heart. tks for my dear family's ok. Listening more means not more talkive. 版权声明: ...