标签:tostring oop 返回结果 cal auto 主线程 running 检查 out
在再度温习Java5的并发编程的知识点时发现,首要的就是把Runnable、Callable、Executor、Future等的关系搞明白,遂有了下述小测试程序,通过这个例子上述三者的关系就一目了然了。
在java5以后,一个可以调度执行的线程单元可以有三种方式定义:
Thread、Runnable、Callable,其中Runnable实现的是void run()方法,Callable实现的是 V call()方法,并且可以返回执行结果,其中Runnable可以提交给Thread来包装下,直接启动一个线程来执行,而Callable则一般都是提交给ExecuteService来执行。
简单来说,Executor就是Runnable和Callable的调度容器,Future就是对于具体的调度任务的执行结果进行查看,最为关键的是Future可以检查对应的任务是否已经完成,也可以阻塞在get方法上一直等待任务返回结果。Runnable和Callable的差别就是Runnable是没有结果可以返回的,就算是通过Future也看不到任务调度的结果的。
- package com.hadoop.thread;
-
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
-
- public class RunnableAndCallable2Future {
-
- public static void main(String[] args) {
-
-
- ExecutorService executor = Executors.newFixedThreadPool(3);
- try {
-
-
- Future<?> runnable1 = executor.submit(new Runnable() {
- @Override
- public void run() {
- System.out.println("runnable1 running.");
- }
- });
- System.out.println("Runnable1:" + runnable1.get());
-
-
-
-
- Future<String> future1 = executor.submit(new Callable<String>() {
- @Override
- public String call() throws Exception {
-
- return "result=task1";
- }
- });
-
- System.out.println("task1: " + future1.get());
-
-
-
-
- Future<String> future2 = executor.submit(new Callable<String>() {
- @Override
- public String call() throws Exception {
- try {
- while (true) {
- System.out.println("task2 running.");
- Thread.sleep(50);
- }
- } catch (InterruptedException e) {
- System.out.println("Interrupted task2.");
- }
- return "task2=false";
- }
- });
-
-
- Thread.sleep(10);
- System.out.println("task2 cancel: " + future2.cancel(true));
-
-
-
-
- Future<String> future3 = executor.submit(new Callable<String>() {
-
- @Override
- public String call() throws Exception {
- throw new Exception("task3 throw exception!");
- }
-
- });
- System.out.println("task3: " + future3.get());
- } catch (Exception e) {
- System.out.println(e.toString());
- }
-
- executor.shutdownNow();
- }
- }
执行结果如下:
- runnable1 running.
- Runnable1:null
- task1: result=task1
- task2 running.
- task2 cancel: true
- Interrupted task2.
- java.util.concurrent.ExecutionException: java.lang.Exception: Bad flag value!
FutureTask则是一个RunnableFuture<V>,即实现了Runnbale又实现了Futrue<V>这两个接口,另外它还可以包装Runnable和Callable<V>,所以一般来讲是一个符合体了,它可以通过Thread包装来直接执行,也可以提交给ExecuteService来执行,并且还可以通过v get()返回执行结果,在线程体没有执行完成的时候,主线程一直阻塞等待,执行完则直接返回结果。
- public class FutureTaskTest {
-
-
- public static void main(String[] args) {
- Callable<String> task = new Callable<String>() {
- public String call() {
- System.out.println("Sleep start.");
- try {
- Thread.sleep(1000 * 10);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- System.out.println("Sleep end.");
- return "time=" + System.currentTimeMillis();
- }
- };
-
-
- FutureTask<String> ft = new FutureTask<String>(task);
- Thread t = new Thread(ft);
- t.start();
- try {
- System.out.println("waiting execute result");
- System.out.println("result = " + ft.get());
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- } catch (ExecutionException e) {
-
- e.printStackTrace();
- }
-
-
- System.out.println("=========");
- FutureTask<String> ft2 = new FutureTask<String>(task);
- Executors.newSingleThreadExecutor().submit(ft2);
- try {
- System.out.println("waiting execute result");
- System.out.println("result = " + ft2.get());
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- } catch (ExecutionException e) {
-
- e.printStackTrace();
- }
-
- }
- }
执行结果如下:
- waiting execute result
- Sleep start.
- Sleep end.
- result = time=1370844662537
- =========
- waiting execute result
- Sleep start.
- Sleep end.
- result = time=1370844672542
Runnable、Callable、Executor、Future、FutureTask关系解读
标签:tostring oop 返回结果 cal auto 主线程 running 检查 out
原文地址:http://www.cnblogs.com/cristin/p/7746018.html