java 并发编程java并发编程

Java并发之可携带结果的任务:Callable和Future

2017-09-29  本文已影响22人  Jiafu

Callable与 Future 两功能是Java 5版本中加入的,Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务。
Callable的接口定义如下:

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

Callable和Runnable的区别如下:

Future 介绍
Future表示异步计算的结果,它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。Future的cancel方法可以取消任务的执行,它有一布尔参数,参数为 true 表示立即中断任务的执行,参数为 false 表示允许正在运行的任务运行完成。Future的 get 方法等待计算完成,获取计算结果。

下面的示例中,我们使用Callable来提交任务,使用Future来得到执行的结果。
Java示例:

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
 
public class CallableAndFuture {
 
    public static void main(String[] args) {
        // An example of Future.
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        Future<String> future = threadPool.submit(new Callable<String>() {
            public String call() throws Exception {
                Thread.sleep(2000);
                return "Hello";
            };
        });
        System.out.println("Wait for result...");
        try {
            System.out.println("Get the result:" + future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        threadPool.shutdown();
 
        ExecutorService threadPool2 = Executors.newFixedThreadPool(10);
        // CompletionService is a service that decouples the production of new
        // asynchronous tasks from the consumption of the results of completed
        // tasks. Producers submit tasks for execution. Consumers take completed
        // tasks and process their results in the order they complete.
        CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(
                threadPool2);
 
        // submit 10 tasks
        System.out.println("Submit 10 tasks.");
        for (int i = 1; i <= 10; i++) {
            final int seq = i;
            completionService.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    Thread.sleep(new Random().nextInt(5000));
                    return seq;
                }
            });
        }
 
        System.out.println("Wait for completion.");
        // get the result by order
        for (int i = 0; i < 10; i++) {
            try {
                System.out.println("Task " + completionService.take().get()
                        + " completed.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        threadPool2.shutdown();
    }
}
上一篇下一篇

猜你喜欢

热点阅读