于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. 【Spring】Spring学习笔记-01-入门级实例

    听说当前Spring框架很流行,我也准备好好学学Spring开发,并将学习的过程和大家分享,希望能对志同道合的同学有所帮助. 以下是我学习Spring的第一个样例. 1.Spring开发环境的搭建 我 ...

  2. Java下拼接执行动态SQL语句(转)

    在实际业务中经常需要拼接动态SQL来完成复杂数据计算,网上各类技术论坛都有讨论,比如下面这些问题: http://bbs.csdn.net/topics/390876591 http://bbs.cs ...

  3. 怎样解决No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).

    怎样解决No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386). 错误 ...

  4. Microsoft Toolkit 2.5下载 – 一键激活Windows 8.1/2012 R2/Office 2013

    http://www.dayanzai.me/microsoft-toolkit-2-5.html

  5. 轻松搭建Windows8云平台开发环境

    原文:轻松搭建Windows8云平台开发环境 Windows Store应用是基于Windows 8操作系统的新一代Windows应用程序,其开发平台以及运行模式和以往传统平台略有不同.为了帮助更多开 ...

  6. Python计算&绘图——曲线拟合问题(转)

    题目来自老师的课后作业,如下所示.很多地方应该可以直接调用函数,但是初学Python,对里面的函数还不是很了解,顺便带着学习的态度,尽量自己动手code. 测试版代码,里面带有很多注释和测试代码: # ...

  7. BZOJ3362 [Usaco2004 Feb]Navigation Nightmare 导航噩梦

    标题效果:自脑补. 思维:与维护两个维度和可设置为检查右. 注意,标题给予一堆关系的.我们应该加入两对关系. Code: #include <cstdio> #include <cs ...

  8. SQLSERVER存储过程语法的具体解释

    SQL SERVER存储过程语法: Create PROC [ EDURE ] procedure_name [ ; number ]     [ { @parameter data_type }   ...

  9. 最艰难的采访IT公司ThoughtWorks代码挑战——FizzBuzzWhizz游戏

    最近的互联网招聘平台拉勾网在五月推出了"最艰难的采访IT公司"码挑战活动,评选出了5个最难面试的IT公司,即:ThoughtWorks.Google.Unisys.Rackspac ...

  10. Android提高第二篇之SurfaceView的基本使用

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 上次介绍MediaPlayer的时候略微介绍了SurfaceView,SurfaceView因为能够直接从内存 ...