于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. 创建Oracle的用户 create user scott identified by tiger;

    在命令行里sqlplus 以system身份登录,password是自己设的system C:\Users\Administrator>sqlplus SQL*Plus: Release 10. ...

  2. 利用__index和__newindex实现默认值表、监控表、只读表(转)

    __index和__newindex实际上相当于是在读写表的时候分别加了一道过滤的逻辑,让读写表的操作可以被监控或说回调,利用这个特性可以实现一些带有特殊功能的表. 带有默认值的表: setdefau ...

  3. Android-管理Activity生命周期 -开始一个Activity

    很多程序都是从main()方法开始启动的,和其他程序不同,android是在activity生命周期的特定状态的特定回调方法中初始化代码的.activity启动和销毁的时候都用很多回调方法. 这里将要 ...

  4. 开源Math.NET基础数学类库使用(12)C#随机数扩展方法

    原文:[原创]开源Math.NET基础数学类库使用(12)C#随机数扩展方法                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...

  5. 【Android进阶】判断网络连接状态并自动界面跳转

    用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面 /** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protecte ...

  6. 【Android基础】单元测试的配置

    1.在AndroidManifest清单文件中进行配置 <application android:allowBackup="true" android:debuggable= ...

  7. 11gRAC CHM 管理

    Cluster Health Monitor(缩写CHM)是Oracle提供的工具,自己主动的资源来收集操作系统(CPU.内存.SWAP.过程.I/O与网络)用法. CHM数据被收集每秒一次,11.2 ...

  8. unity3d c# 产生真正的随机数

    虽然能够使用Random类来生成随机数.但它是系统时钟种子,因此,有大量的反复产生伪随机数的. 您可以使用RNGCryptoServiceProvider();相对真随机数生成. 由加密服务提供程序( ...

  9. 【翻译自mos文章】rman 备份时报:ORA-02396: exceeded maximum idle time

    rman 备份时报:ORA-02396: exceeded maximum idle time 參考原文: RMAN backup faling with ORA-02396: exceeded ma ...

  10. [译]MVC应用程序生命周期

    原文:MVC Application Lifecycle 来一探究竟在MVC应用程序中参与请求处理的各个不同组件. 目录: 序言 背景 UrlRoutingModule RouteHandler Mv ...