于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. TFS(Team Foundation Server)简介和新手入门

    在两部分的文章.我会介绍Team Foundation Server一些核心功能,着重于产品的日常应用是如何将这些功能结合使用. 作为一个软件开发.在我的职业生涯,.我常常用于支持软件开发过程中大量的 ...

  2. iOS_18_开关控制器_NavigationController_push道路_数据传输

    最后效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill ...

  3. 微软研究院的分布式云计算框架orleans

    orleans   Orleans 客户端请求的消息流转以及消息在Silo中再路由机制 Witte 2015-04-29 21:58 阅读:196 评论:0     一种基于Orleans的分布式Id ...

  4. 【Android基础】Activity之间进行参数传递的三种方式

    1.使用Intent进行传输 //发送数据的Activity class button implements OnClickListener{ @Override public void onClic ...

  5. MySQL 正則表達式搜索

    products表例如以下: 1. 基本字符匹配 使用正則表達式与LIKE的差别,正則表達式是在整个列搜索,仅仅要prod_name中包括了所搜索的字符就能够了,而LIKE假设不用通配符,那么要求pr ...

  6. atitit.(设计模式1)--—职责链(chain of responsibility)最佳实践O7 转换日期

    atitit.设计模式(1)---职责链模式(chain of responsibility)最佳实践O7 日期转换 1. 需求:::日期转换 1 2. 能够选择的模式: 表格模式,责任链模式 1 3 ...

  7. Cocos2d-x 3.x plist+png 做动画

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  8. PL/SQL Developer ORA-12154: TNS: 无法解析指定的连接标识符

    底:         在这台机器(Win7 64位置  最后)设备Oracle 11g的client(已安装32位ORACLEclient.假设安装64位ORACLEclient的时候,在CMD命令中 ...

  9. android 反编译,反,注射LOG

    反编译smali注射显示LOG该代码.以后使用: .class public Lnet/iaround/connector/DebugClass; .super Ljava/lang/Object; ...

  10. C#-默认显示前列-ShinePans

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...