Callable和Future

Callable

相对于Runnable,Callable并不是很让人熟知,其实Callable和Runnable很类似,只不过它有返回值,并且也没有run()方法,而是有call()方法。

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

Callable 接口类似于 Runnable,两者都是为那些其实例可能被另一个线程执行的类设计的。但是 Runnable 不会返回结果,并且无法抛出经过检查的异常。

可以看到,返回的结果是以V泛型表示的,比如Callable<Integer>表示一个最终返回Integer的异步计算

Future

Future用来保存异步计算的结果,就是说之前用Callable标志的任务可以用Future来进行包装,那为什么非要用Future呢,Callable自己运行然后用相应的类型来接收结果不就行了吗?之所以要用到Future,有一下两个原因:

  1. Thread t = new Thread(..)用这个方法创建一个线程,必须要传给一个Runnable的参数,而不能传给它Callable
  2. Future对象具有一系列的方法,比如它的get方法能够被阻塞,直到计算完成;也可以在任务进行的过程中取消它。这些方法使得它更便利

FutureTask

但是Future终究只是一个接口,而FutureTask包装器不仅实现了Future接口,还实现了Runnable接口,这弥补上面的遗憾,使得它不仅能被Thread运行,还具有取消运行的特性,一个典型的使用FutureTask的例子就是:

Callable<Integer> calc = ...;
FutureTask<Integer> task = new FutureTask<Integer>(calc);
Thread t = new Thread(task);   //这里是Runnable
t.start;
...
Integer result = task.get();   //这里是Future

住:FutureTask的两个构造方法

FutureTask(Callable task)
FutureTask(Runnable task,V result)

Demo

一个计算指定目录下具有指定关键字的文件数目的例子:

package future;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class MatchCounter implements Callable<Integer> {

    private File directory;
    private String keyword;
    private int count;

    public MatchCounter(File directory, String keyword) {
        this.directory = directory;
        this.keyword = keyword;
    }

    @Override
    public Integer call() throws Exception {
        count = 0;
        try {
            File[] files = directory.listFiles();
            List<Future<Integer>> results = new ArrayList<>(); // 用来保存所有的异步计算结果

            for (File file : files) {
                if (file.isDirectory()) {
                    MatchCounter counter = new MatchCounter(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> r : results) {
                count += r.get();
            }
        } catch (InterruptedException e) {

        }
        return count;
    }

    public boolean search(File file) {
        try {
            Scanner in = new Scanner(file);
            boolean found = false;
            while (in.hasNextLine()) {
                String line = in.nextLine();
                if (line.contains(keyword)) {
                    found = true;
                }
            }
            return found;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;

    }
}

测试类:

package future;

import java.io.File;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter starting directory:");
        String d = in.nextLine();
        System.out.println("Enter keyword:");
        String keyword = in.nextLine();

        MatchCounter counter = new MatchCounter(new File(d), keyword);
        FutureTask<Integer> task = new FutureTask<>(counter);

        new Thread(task).start();

        try {
            System.out.println(task.get() + " matching files!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

运行:

Enter starting directory:
D:\workspace\concurrent\src
Enter keyword:
future
2 matching files!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值