Callable Future Executor
2016-05-14 20:38
423 查看
一般通过继承Thread还有实现Runnable,都不可以有返回值,不可以声明检查异常。
Callable和Future,通过ExecutorService的submit方法执行Callable,并返回给Future,这里返回值只有一个,ExecutorService继承自Executor,通过使用Executor执行器来操作。
接下来是执行多个带返回值的任务,并取得多个返回值:
可以使用CompletionService,你要多个值那就来一个Future集合
Callable和Future,一个产生结果,一个拿到结果。 Callable接口类似于Runnable,但是Runnable不会返回结果,并且无法抛出返回结果的异常,而Callable功能更强大一些,被线程执行后,可以返回值,Future可以拿到异步执行任务的返回值。
`
FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值
FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值,那么这个组合的使用有什么好处呢?假设有一个很耗时的返回值需要计算,并且这个返回值不是立刻需要的话,那么就可以使用这个组合,用另一个线程去计算返回值,而当前线程在使用这个返回值之前可以做其它的操作,等到需要这个返回值时,再通过Future得到。
原文地址:http://blog.csdn.net/ghsau/article/details/7451464,转载请注明
Callable和Future,通过ExecutorService的submit方法执行Callable,并返回给Future,这里返回值只有一个,ExecutorService继承自Executor,通过使用Executor执行器来操作。
public class CallableAndFuture { public static void main(String[] args) { ExecutorService threadPool = Executors.newSingleThreadExecutor(); Future<Integer> future = threadPool.submit(new Callable<Integer>() {//提交线程,需要个线程。 public Integer call() throws Exception { return new Random().nextInt(100); } }); }
接下来是执行多个带返回值的任务,并取得多个返回值:
可以使用CompletionService,你要多个值那就来一个Future集合
public class CallableAndFuture { public static void main(String[] args) { ExecutorService threadPool = Executors.newCachedThreadPool(); CompletionService<Integer> cs = new ExecutorCompletionService<Integer>(threadPool); for(int i = 1; i < 5; i++) { final int taskID = i; cs.submit(new Callable<Integer>() { public Integer call() throws Exception { return taskID; } }); } // 可能做一些事情 for(int i = 1; i < 5; i++) { try { System.out.println(cs.take().get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } }
Callable和Future,一个产生结果,一个拿到结果。 Callable接口类似于Runnable,但是Runnable不会返回结果,并且无法抛出返回结果的异常,而Callable功能更强大一些,被线程执行后,可以返回值,Future可以拿到异步执行任务的返回值。
`
`` public class CallableAndFuture { public static void main(String[] args) { Callable<Integer>c=new Callable<Integer>( ) { @Override public Integer call() throws Exception { // TODO Auto-generated method stub return new Random().nextInt(10); } }; FutureTask<Integer>f=new FutureTask<Integer>(c); } }
FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值
FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值,那么这个组合的使用有什么好处呢?假设有一个很耗时的返回值需要计算,并且这个返回值不是立刻需要的话,那么就可以使用这个组合,用另一个线程去计算返回值,而当前线程在使用这个返回值之前可以做其它的操作,等到需要这个返回值时,再通过Future得到。
原文地址:http://blog.csdn.net/ghsau/article/details/7451464,转载请注明
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树