java中Callable接口是使用示例

来源:互联网 发布:淘宝充流量 编辑:程序博客网 时间:2024/06/06 02:05

与Runnable区别:

        Runnable:没有返回信息,实现run()方法。

        Callable<T>: A task that returns a result and may throw an exception ,返回执行结果,抛出异常;需要实现call()方法

Future

        Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作。get方法会阻塞,直到任务返回结果

FutureTask

         FutureTask实现了Runnbale又实现了Futrue<V>这两个接口,既是Future、Runnable,又包装了Callable。


eg:

import java.util.ArrayList;import java.util.List;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;import java.util.concurrent.FutureTask;public class CallableTest{ public static void main(String[] args) throws InterruptedException, ExecutionException {//1 。因此FutureTask既是Future、 Runnable,又是包装了Callable( 如果是Runnable最终也会被转换为Callable ), 它是这两者的合体。FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return new Random().nextInt(100);}});new Thread(futureTask).start();System.out.println(futureTask.get());System.out.println("###########################################################");//2 Executor就是Runnable和Callable的调度容器,Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作ExecutorService threadPool = Executors.newSingleThreadExecutor();Future<Integer> future = threadPool.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return new Random().nextInt(100);}});//get方法会阻塞,直到任务返回结果System.out.println(future.get());System.out.println("###########################################################");//3 Future是按照添加的顺序排列的ExecutorService threadPool1 = Executors.newSingleThreadExecutor();List<Future<Integer>> list = new ArrayList<>();for(int i=0;i<5;i++){list.add(threadPool1.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return new Random().nextInt(100);}}));}for(Future<Integer> fs:list){System.out.println(fs.get());}System.out.println("------------------------");ArrayList<Callable<Integer>> arrayList = new ArrayList<Callable<Integer>>();for(int i=0;i<5;i++){arrayList.add(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return new Random().nextInt(100);}});}List<Future<Integer>> result = threadPool1.invokeAll(arrayList);for(Future<Integer> fs:result){System.out.println(fs.get());}System.out.println("###########################################################");//4  提交到CompletionService中的Future是按照完成的顺序排列的ExecutorService executorService = Executors.newSingleThreadExecutor();CompletionService<Integer> cs = new ExecutorCompletionService<>(executorService);for(int i=0;i<5;i++){final int id = i;cs.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return id;}});}for(int i=0;i<5;i++){System.out.println(cs.take().get());}System.out.println("###########################################################");}}


0 0
原创粉丝点击