于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同步和异步帧的更多相关文章

  1. Java:多线程,线程同步,同步锁(Lock)的使用(ReentrantLock、ReentrantReadWriteLock)

    关于线程的同步,可以使用synchronized关键字,或者是使用JDK 5中提供的java.util.concurrent.lock包中的Lock对象.本文探讨Lock对象. synchronize ...

  2. Java 并发编程 -- Fork/Join 框架

    概述 Fork/Join 框架是 Java7 提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.下图是网上流传的 Fork Join 的 ...

  3. java 中的fork join框架

    文章目录 ForkJoinPool ForkJoinWorkerThread ForkJoinTask 在ForkJoinPool中提交Task java 中的fork join框架 fork joi ...

  4. JUC组件扩展(二)-JAVA并行框架Fork/Join(二):同步和异步

    在Fork/Join框架中,提交任务的时候,有同步和异步两种方式. invokeAll()的方法是同步的,也就是任务提交后,这个方法不会返回直到所有的任务都处理完了. fork方法是异步的.也就是你提 ...

  5. Java并发编程--Fork/Join框架使用

    上篇博客我们介绍了通过CyclicBarrier使线程同步,可是上述方法存在一个问题,那就是假设一个大任务跑了2个线程去完毕.假设线程2耗时比线程1多2倍.线程1完毕后必须等待线程2完毕.等待的过程线 ...

  6. JUC组件扩展(二)-JAVA并行框架Fork/Join(四):监控Fork/Join池

    Fork/Join 框架是为了解决可以使用 divide 和 conquer 技术,使用 fork() 和 join() 操作把任务分成小块的问题而设计的.主要实现这个行为的是 ForkJoinPoo ...

  7. JUC组件扩展(二)-JAVA并行框架Fork/Join(一):简介和代码示例

    一.背景 虽然目前处理器核心数已经发展到很大数目,但是按任务并发处理并不能完全充分的利用处理器资源,因为一般的应用程序没有那么多的并发处理任务.基于这种现状,考虑把一个任务拆分成多个单元,每个单元分别 ...

  8. Java 并发之 Fork/Join 框架

    什么是 Fork/Join 框架 Fork/Join 框架是一种在 JDk 7 引入的线程池,用于并行执行把一个大任务拆成多个小任务并行执行,最终汇总每个小任务结果得到大任务结果的特殊任务.通过其命名 ...

  9. Java并行任务框架Fork/Join

    Fork/Join是什么? Fork意思是分叉,Join为合并.Fork/Join是一个将任务分割并行运行,然后将最终结果合并成为大任务的结果的框架,父任务可以分割成若干个子任务,子任务可以继续分割, ...

随机推荐

  1. Linux 环境下 Lua 安装(转)

    系统环境:CentOS-6.2-x86_64. Lua 是嵌入式脚本语言,应用场景很广泛. 引自官网:Lua is used in many products and projects around ...

  2. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第7章节--打包并部署SP2013 Apps 打包并公布App

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第7章节--打包并部署SP2013 Apps 打包并公布App         如今既然你理解了一个app的四个主要部分, ...

  3. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  4. 【C语言探索之旅】 第一部分第九课:函数

    内容简介 1.课程大纲 2.第一部分第九课:函数 3.第一部分第十课预告: 练习题+习作 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. ...

  5. 追索权 Eclipse + NDK error: stray &#39;\24&#39; in program

    [size=16px][b][color=#FF0000]追索权 Eclipse + NDK  error: stray '\24' in program[/color][b][/b][/b][/si ...

  6. php阅读csv文件类

    php处理csv文件类: http://www.php100.com/cover/php/540.html <?php define("CSV_Start", 0); def ...

  7. BigPipe设计原理

    高性能页面加载技术--BigPipe设计原理及Java简单实现 1.技术背景 动态web网站的历史可以追溯到万维网初期,相比于静态网站,动态网站提供了强大的可交互功能.经过几十年的发展,动态网站在互动 ...

  8. Android docs4.3API

    查找在线课程,加速进入Android docs API,最主要的原因是网上加载js文件速度慢,另一种是装google字体缓慢! import java.io.BufferedReader; impor ...

  9. 为什么在Python3.4.1里输入print 10000L或10000L失败

    打开Python的命令行交互窗体,而且在里面进行以下的输入: Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 ...

  10. 一张漫画说尽IT开发过程