美文网首页
J.U.C-FutureTask

J.U.C-FutureTask

作者: 墨平语凡 | 来源:发表于2018-06-11 16:39 被阅读0次

    创建线程有两种方式:继承Thread和实现Runnable接口
    但是都有一个共同的缺陷:

    执行完任务后,无法获取执行结果
    通过CallableFuture可以得到任务执行完之后的结果

    • CallableRunnable接口对比

    Callable是泛型的接口,其中有一个call函数, call函数的返回类型就是创建Callable时传进去的类型

    • Future也是以个接口, 对于任务可以进行取消, 查询是否执行完成等, 可以监视目标线程调用call的情况
    package io.haitaoc.concurrency.example.aqs;
    
    import java.util.concurrent.*;
    
    public class FutureExample {
    
        // 返回类型是String
        static class MyCallable implements Callable<String> {
    
            @Override
            public String call() throws Exception {
                System.out.println("do something in callable");
                Thread.sleep(5000);
                return "Done";
            }
        }
    
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            ExecutorService executorService = Executors.newCachedThreadPool();
            // 通过Future接收另外一个线程计算的结果
            Future<String> future = executorService.submit(new MyCallable());
            System.out.println("do something in main");
            Thread.sleep(1000);
            String result = future.get();
            System.out.println(result);
        }
    }
    
    
    • FutureTask父类是RunnableFuture, 执行Callable类型的任务,并且有Future的特性
    package io.haitaoc.concurrency.example.aqs;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    public class FutureTaskExample {
    
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            FutureTask<String>  futureTask = new FutureTask<String>(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    System.out.println("do something in callable");
                    Thread.sleep(5000);
                    return "Done";
                }
            });
    
            // 启动任务
            new Thread(futureTask).start();
            System.out.println("do something in main");
            Thread.sleep(1000);
            String result = futureTask.get();
            System.out.println(result);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:J.U.C-FutureTask

          本文链接:https://www.haomeiwen.com/subject/fuyheftx.html