Callable与Future

  • Runnable封装一个异步运行的任务,可以把它想象成为一个没有参数和返回值的异步方法。CallableRunnable类似,但是有返回值。Callable接口是一个参数化的类型,只有一个方法call
public interface Callable<V> {
	//计算并返回结果
    V call() throws Exception;
}
  • 类型参数是返回值类型。例如,Callable<Integer>表示一个最终返回Integer对象的异步计算。
  • Future保存异步计算的结果。可以启动一个计算,将Future对象交给某个线程,然后忘掉他。Future对象的所有者在结果计算好之后就可以获得它。
  • Future接口具有的方法
public class FutureTest implements Future {
    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return false;
    }

    @Override
    public Object get() throws InterruptedException, ExecutionException {
        return null;
    }

    @Override
    public Object get(long timeout, TimeUnit unit) throws InterruptedException
    								, ExecutionException, TimeoutException {
        return null;
    }
}
  • 第一个get方法的调用被阻塞,直到计算完成。如果在计算完成之前,第二个方法的调用超时,抛出一个TimeoutException异常。如果运行该计算的线程被中断,两个方法都将抛出InterruptedException。如果计算已经完成,那么get方法立即返回。
  • 如果计算还在进行,isDone方法返回false;如果完成了,则返回true
  • 可以用cancel方法取消该计算。如果计算还没有开始,它被取消且不再开始。如果计算处于运行之中,那么如果mayInterrupt参数为true,它就被中断。
  • FutureTask包装器是一种非常便利的机制,可将Callable转换成FutureRunnable,它同时实现两者的接口。如下:
Callable<Integer> myComputation = null;
FutureTask<Integer> task = new FutureTask<Integer>(myComputation);
Thread t = new Thread(task);
t.start();
...
Integer integer = task.get();
  • 提供一端程序代码。这个程序与前面那个寻找包含指定关键字的文件的例子相似。然而,现在我们仅仅计算匹配的文件数目。
public class FutureTest {
    public static void main(String[] args) {
        try(Scanner in = new Scanner(System.in)){
            System.out.println("Enter base directory (e.g /usr/local/jdk5.0/src)");
            String directory = in.nextLine();
            System.out.println("Enter keyword (e.g volatile):");
            String keyword = in.nextLine();
            MachCounter counter = new MachCounter(new File(directory), keyword);
            FutureTask<Integer> task = new FutureTask<>(counter);
            Thread t = new Thread(task);
            t.start();
            try {
                System.out.println(task.get()+" matching files.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }

    static class MachCounter implements Callable<Integer>{
        private File directory;
        private String keyword;
        public MachCounter(File directory,String keyword) {
            this.directory = directory;
            this.keyword = keyword;
        }
        @Override
        public Integer call() throws Exception {
            int count = 0;
            try {
                File[] files = directory.listFiles();
                List<Future<Integer>> results = new ArrayList<>();
                for (File file : files)
                    if (file.isDirectory()) {
                        MachCounter counter = new MachCounter(file, keyword);
                        FutureTask<Integer> task = new FutureTask<>(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){
                e.printStackTrace();
            }
            return count;
        }

        public boolean search(File file){
            try(Scanner in = new Scanner(file,"UTF-8")) {
                boolean found = false;
                while (!found&&in.hasNext()){
                    String line = in.nextLine();
                    if(line.contains(keyword)) found = true;
                }
                return found;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return false;
        }
    }
}
  • 然后,利用MachCounter 创建一个FutureTask对象,并用来启动一个线程。
  • 在call方法内部,使用相同的递归机制。对于每一个子目录,我们产生一个新的MachCounter并为它启动一个线程。此外,把FutureTask对象隐藏在ArrayList<Future<Integer>>中,最后,把所有结果加起来。
  • 每一次对get的调用都会发生阻塞直到结果可获得为止。当然,线程是并行运行的,因此,很可能在大致相同的时刻所有的结果都可获得。
  • 代码最精妙之处在于根据子目录数目先创建线程并开启线程,将线程存储在List中,再通过迭代List为每个线程分配任务。
if (file.isDirectory()) {
	  MachCounter counter = new MachCounter(file, keyword);
	  FutureTask<Integer> task = new FutureTask<>(counter);
	  results.add(task);
	  Thread t = new Thread(task);
	  t.start();
} else {
 	 if (search(file)) count++;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值