您的位置:首页 > 其它

基于Callable和Future的匹配文件数量计算实例

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

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


类型参数是返回值的类型。例如,Callable<Integer>代表一个最终返回Integer对象的异步计算。
Future保存异步计算的结果。当你使用Future对象时,你就可以启动一个计算,把计算结果给某线程,然后就去干你自己的事。Future对象的所有者在结果计算好之后就可以得到它。
Future接口具有下面的方法:

public interface Future<V>
{
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<Integer> myComputation = ...;
FutureTask<Integer> task = new FutureTask<Integer>(myComputation);
Thread t = new Thread(task);//it's a Runnable
t.start();
...
Integer result = task.get();//it's a Future


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

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


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

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


最后,我们打印出结果:

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


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

for(Future<Integer> 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<Integer> task = new FutureTask<Integer>(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<Integer>
{
/**
* 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<Future<Integer>> results = new ArrayList<Future<Integer>>();

for (File file : files)
if (file.isDirectory())
{
MatchCounter counter = new MatchCounter(file, keyword);
FutureTask<Integer> task = new FutureTask<Integer>(counter);
results.add(task);
Thread t = new Thread(task);
t.start();
}
else
{
if (search(file)) count++;
}

for (Future<Integer> 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: