博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Callable和Future的匹配文件数量计算实例
阅读量:4577 次
发布时间:2019-06-08

本文共 4390 字,大约阅读时间需要 14 分钟。

Runnable封装一个异步运行的任务;你可以把它想像成一个没有任何参数和返回值的异步方法。Callable和Runnable相似,但它有返回值。Callable接口是一个参数化的类型,只有一个方法call。

public interface Callable
{ V call() throws Exception;}

类型参数是返回值的类型。例如,Callable<Integer>代表一个最终返回Integer对象的异步计算。

   Future保存异步计算的结果。当你使用Future对象时,你就可以启动一个计算,把计算结果给某线程,然后就去干你自己的事。Future对象的所有者在结果计算好之后就可以得到它。
   Future接口具有下面的方法:   

public interface Future
{ V get() throws ...; V get(long timeout, Timeout unit) throws ...; void cancel(boolean mayInterrupt); boolean isCancelled(); boolean isDone();}

第一个get方法的调用将被阻塞,直至计算完成。第二个get方法的调用如果在计算完成之前超时,那么将抛出TimeoutException异常。如果运行计算的线程被中断,这两个方法都将抛出InterruptedException异常。如果计算已经完成,那么get方法将立即返回。

   如果计算还在进行中,isDone方法将返回false,如果计算已经完成则返回true。
   你可以使用cancel方法取消计算。如果计算还没开始,它会被取消并永远不会开始。如果计算正在进行,那么,如果mayInterrupt参数值为true,它就会被中断。
   FutureTask包装器是一种很方便的将Callable转换成Future和Runnable的机制,它同时实现了两者的接口。例如,

Callable
myComputation = ...;FutureTask
task = new FutureTask
(myComputation);Thread t = new Thread(task);//it's a Runnablet.start();...Integer result = task.get();//it's a Future

下面的程序中使用了这些概念。这个程序与前面那个寻找包含指定关键字文件的例子相似。但是,现在我们仅仅是计算匹配的文件数量。因此,我们有了一个长时间运行的任务,它产生一个整数值,一个Callable<Integer>的例子。

class MatchCounter implements Callable
{ public MatchCounter(File directory,String keyword){...} public Integer call() {...} //return the number of matching files}

然后我们利用MatchCounter创建一个FutureTask对象,并使用它来启动一个线程。  

FutureTask
task = new FutureTask
(counter);Thread t = new Thread(task);t.start();

 最后,我们打印出结果:

System.out.println(task.get()+" matching files.");

当然,对get的调用会发生阻塞知道有可获得的结果为止。

在call方法内部,我们使用相同的递归机制。对于每一个子目录,我们产生一个MatchCounter并为它启动一个线程。此外,我们还把FutureTask对象隐藏在ArrayList<Integer>中。最后,我们把所有结果加起来:

for(Future
result: results) count += result.get();

每次对get的调用都会发生阻塞直到有结果可获得为止。当然,线程是并行运行的,因此很有可能在大致相同的时刻,所有的结果都可获得。

import java.io.*;import java.util.*;import java.util.concurrent.*;public class FutureTest{   public static void main(String[] args)   {      Scanner in = new Scanner(System.in);      System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): ");      String directory = in.nextLine();      System.out.print("Enter keyword (e.g. volatile): ");      String keyword = in.nextLine();      MatchCounter counter = new MatchCounter(new File(directory), keyword);      FutureTask
task = new FutureTask
(counter); Thread t = new Thread(task); t.start(); try { System.out.println(task.get() + " matching files."); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { } }}/** * This task counts the files in a directory and its subdirectories that contain a given keyword. */class MatchCounter implements Callable
{ /** * Constructs a MatchCounter. * @param directory the directory in which to start the search * @param keyword the keyword to look for */ public MatchCounter(File directory, String keyword) { this.directory = directory; this.keyword = keyword; } public Integer call() { count = 0; try { File[] files = directory.listFiles(); ArrayList
> results = new ArrayList
>(); for (File file : files) if (file.isDirectory()) { MatchCounter counter = new MatchCounter(file, keyword); FutureTask
task = new FutureTask
(counter); results.add(task); Thread t = new Thread(task); t.start(); } else { if (search(file)) count++; } for (Future
result : results) try { count += result.get(); } catch (ExecutionException e) { e.printStackTrace(); } } catch (InterruptedException e) { } return count; } /** * Searches a file for a given keyword. * @param file the file to search * @return true if the keyword is contained in the file */ public boolean search(File file) { try { Scanner in = new Scanner(new FileInputStream(file)); boolean found = false; while (!found && in.hasNextLine()) { String line = in.nextLine(); if (line.contains(keyword)) found = true; } in.close(); return found; } catch (IOException e) { return false; } } private File directory; private String keyword; private int count;}

 

转载于:https://www.cnblogs.com/XL-Liang/archive/2012/06/13/2548032.html

你可能感兴趣的文章
数据适配 DataAdapter对象
查看>>
有序列表ol和定义列表dl,dt,dd
查看>>
联想小新Air 15 安装黑苹果macOS High Sierra 10.13.6过程
查看>>
公共POI导出Excel方法–java
查看>>
次短路——Dijkstra
查看>>
C++ compile issue
查看>>
安卓中的shape
查看>>
站立会议总结08
查看>>
C++ stat判断路径是文件还是目录
查看>>
动态代理
查看>>
ie11下,接受postmessage返回的信息
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第1节 继承_13-Java继承的三个特点...
查看>>
中小企业实施OA的意义
查看>>
es6 数组
查看>>
JS判断是否在微信浏览器打开
查看>>
javascript中typeof和instanceof的区别
查看>>
数据结构-数组1
查看>>
jquery之别踩白块游戏的实现
查看>>
转载Eclipse中Maven WEB工程tomcat项目添加调试
查看>>
caller和callee的解析与使用-型参与实参的访问
查看>>