美文网首页
异步多线程----Callable和Future

异步多线程----Callable和Future

作者: 爱做梦的严重精神病患者 | 来源:发表于2019-03-19 14:45 被阅读0次

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

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

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

  Future保存异步计算的结果。可以启动一个计算,将Future对象交给某个线程,Future对象的所有者在结果计算好之后就可以获得它。

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

  第一个get方法的调用被阻塞直到计算完成。如果在计算完成之前,第二个方法的调用超时,抛出一个TimeoutException异常。如果运行该计算的线程被中断,两个方法都将抛出InterruptedException。如果计算已经完成,那么get方法立即返回
  如果计算还在进行isDone方法返回false;如果完成了,则返回true
  可以用cancel方法取消该计算。如果计算还没有开始,它被取消且不再开始。如果计算处于运行之中,那么如果mayInterrupt参数为true,它就被中断。

  FutureTask(既是Future又是Runnable)包装器是一种非常便利的机制,可将Callable转换成Future和Runnable,它同时实现二者的接口

Callable<Integer> myComputation = ...;
FutureTask<Integer> task = new FutureTask<Integer>(myComputation);
Thread t = new Thread(task);//体现Runnable特性
t.start();
...
Integer result = task.get();//体现Future特性

  以一个计算匹配的文件数目程序为例:

class MatchCounter implements Callable<Integer>
{
    private File directory;
    private String keyword;

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

    public Integer call() {
          int 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> result : results) {
                    try{
                            count += result.get();  
                        }
                    catch (ExecutionException e) {
                            e.printStackTrace();
                        }
                 }
          }
          catch (InterruptedException e) {}    

          return count;

      }

}

public boolean search(File file) {...}

public class FutureTest {
      public static void main(String[] args) {
          MatchCounter counter = new MatchCounter(new File(directory), keyword);
          FutureTask<Integer> task = new FutureTask<>(counter);
          Thread t = new Thread(task);
          t.start();
      }
}

相关文章

网友评论

      本文标题:异步多线程----Callable和Future

      本文链接:https://www.haomeiwen.com/subject/oazwmqtx.html