Java对多线程~~~Fork/Join同步和异步帧
2015-10-10 15:38
471 查看
于Fork/Join骨架,当提交的任务,有两个同步和异步模式。它已被用于invokeAll()该方法是同步的。是任何
务提交后,这种方法不会返回直到全部的任务都处理完了。而还有还有一种方式,就是使用fork方法,这个是异步的。也
就是你提交任务后,fork方法马上返回。能够继续以下的任务。
这个线程也会继续执行。
以下我们以一个查询磁盘的以log结尾的文件的程序样例来说明异步的使用方法。
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.
务提交后,这种方法不会返回直到全部的任务都处理完了。而还有还有一种方式,就是使用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编程规范
- spring与servlet集成开发对象管理问题解决方法
- JAVA中文乱码简单解决办法
- java设计模式之单例模式
- Spring4MVC使用记录
- @Autowired使用中的问题及@Qualifire的使用
- java 小记
- Java计算某月第几个星期几的日期
- Hadoop II Windows下安装hadoop2.6.0-eclipse-plugin插件
- 【Java】原型模式
- IOS上传文件到JAVA服务器
- 关于weblogic下使用URL.openConnection获取连接返回javax.net.ssl.SSLKeyException错误
- android adt自带eclipse无法设置ndk路径
- 详解Java编程中统一资源定位符URL的相关使用
- JavaSE I/O 流 文件度和写
- java反射机制详解 及 Method.invoke解释
- UIViewAnimationWithBlocks动画之spring
- Java中实现小数的精确计算
- Myeclipse不能编译解决方案
- Java 序列化Serializable详解(附详细例子)