java中callable和futuretask使用
2014-08-14 10:29
330 查看
Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结果,并且无法抛出返回结果的异常,而Callable功能更强大一些,被线程执行后,可以返回值,这个返回值可以被Future拿到,也就是说,Future可以拿到异步执行任务的返回值,下面来看一个简单的例子:
[java]
view plaincopyprint?
public class CallableAndFuture {
public static
void main(String[] args) {
Callable<Integer> callable =
new Callable<Integer>() {
public Integer call()
throws Exception {
return
new Random().nextInt(100);
}
};
FutureTask<Integer> future = new FutureTask<Integer>(callable);
new Thread(future).start();
try {
Thread.sleep(5000);// 可能做一些事情
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
下面来看另一种方式使用Callable和Future,通过ExecutorService的submit方法执行Callable,并返回Future,代码如下:
[java]
view plaincopyprint?
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);
}
});
try {
Thread.sleep(5000);// 可能做一些事情
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
执行多个带返回值的任务,并取得多个返回值,代码如下:
[java]
view plaincopyprint?
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();
}
}
}
}
其实也可以不使用CompletionService,可以先创建一个装Future类型的集合,用Executor提交的任务返回值添加到集合中,最后遍历集合取出数据,代码略。
[java]
view plaincopyprint?
public class CallableAndFuture {
public static
void main(String[] args) {
Callable<Integer> callable =
new Callable<Integer>() {
public Integer call()
throws Exception {
return
new Random().nextInt(100);
}
};
FutureTask<Integer> future = new FutureTask<Integer>(callable);
new Thread(future).start();
try {
Thread.sleep(5000);// 可能做一些事情
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
public class CallableAndFuture { public static void main(String[] args) { Callable<Integer> callable = new Callable<Integer>() { public Integer call() throws Exception { return new Random().nextInt(100); } }; FutureTask<Integer> future = new FutureTask<Integer>(callable); new Thread(future).start(); try { Thread.sleep(5000);// 可能做一些事情 System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值,那么这个组合的使用有什么好处呢?假设有一个很耗时的返回值需要计算,并且这个返回值不是立刻需要的话,那么就可以使用这个组合,用另一个线程去计算返回值,而当前线程在使用这个返回值之前可以做其它的操作,等到需要这个返回值时,再通过Future得到,岂不美哉!这里有一个Future模式的介绍:http://openhome.cc/Gossip/DesignPattern/FuturePattern.htm。
下面来看另一种方式使用Callable和Future,通过ExecutorService的submit方法执行Callable,并返回Future,代码如下:
[java]
view plaincopyprint?
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);
}
});
try {
Thread.sleep(5000);// 可能做一些事情
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
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); } }); try { Thread.sleep(5000);// 可能做一些事情 System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); 4000 } } }代码是不是简化了很多,ExecutorService继承自Executor,它的目的是为我们管理Thread对象,从而简化并发编程,Executor使我们无需显示的去管理线程的生命周期,是JDK 5之后启动任务的首选方式。
执行多个带返回值的任务,并取得多个返回值,代码如下:
[java]
view plaincopyprint?
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();
}
}
}
}
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(); } } } }
其实也可以不使用CompletionService,可以先创建一个装Future类型的集合,用Executor提交的任务返回值添加到集合中,最后遍历集合取出数据,代码略。
相关文章推荐
- Java多线程研究06-带返回值的线程定义接口Callable以及Future,FutureTask的使用
- Java中的Runnable、Callable、Future、FutureTask的区别和CompletionService的使用场景
- Android AsyncTask完全解析FutureTask 深度解析 -Java并发编程:Callable、Future和FutureTask一个使用DownloadManager下载文件的小例
- Java中的Runnable、Callable、Future、FutureTask的区别和CompletionService的使用场景
- java多线程返回值使用示例(callable与futuretask)
- Java中的Runnable、Callable、Future、FutureTask的区别
- java多线程开发,Executors、FutureTask、Callable
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- JAVA 笔记 Callable 与 FutureTask:有返回值的多线程
- java并发--Callable的使用
- Java并发编程:Callable、Future和FutureTask
- Java—Callable,Future的使用
- Java多线程之 Callable、Future和FutureTask
- 【JAVA】java中Future、FutureTask的使用
- java 使用Callable和Future返回线程执行的结果
- Java中的Runnable、Callable、Future、FutureTask的区别与示例
- Java并发编程:Callable、Future和FutureTask(原文链接:http://www.cnblogs.com/dolphin0520/p/3949310.html)
- Java中的Runnable、Callable、Future、FutureTask的区别与示例
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- Java_并发线程_Future、FutureTask、Callable