美文网首页
java并发编程 - 8 - Future

java并发编程 - 8 - Future

作者: cf6bfeab5260 | 来源:发表于2019-05-16 16:02 被阅读0次

    java并发编程- 7 - 线程池这一章我们已经提到过Future,用于接收子线程的返回值。再来回顾一下代码:

    @Test
        public void futureTest() throws ExecutionException, InterruptedException {
            // 创建 FutureTask
            FutureTask<User> futureTask = new FutureTask<User>(()-> {
                        return new User(2222,1000);
            });
            // 创建线程池
            ThreadPoolExecutor es=new ThreadPoolExecutor(20,50,3, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(200),new ThreadPoolExecutor.DiscardOldestPolicy());
            // 提交 FutureTask
            es.submit(futureTask);
            // 获取计算结果
            User result = futureTask.get();
            log.info("result:"+result);
    
        }
    

    这个列子稍微有些简单,我们还是回到组装车子的场景:1、制造车身。2、制造轮子。3、制造车子。

    (省略entity)代码如下:

     @Test
        public void madeCarUseFuture() throws ExecutionException, InterruptedException {
            //制造body
            FutureTask<CarBody> carBodyFutureTask=new FutureTask<CarBody>(new Callable<CarBody>() {
                @Override
                public CarBody call() throws Exception {
                    log.info("madde body start");
                    CarBody body=new CarBody("my carbody");
                    Thread.sleep(2000);
                    log.info("madde body success");
                    return body;
                }
            });
    
    
            //制造轮子
            FutureTask<CarWheel> carWheelFutureTask=new FutureTask<CarWheel>(()->{
                log.info("made wheel start");
                CarWheel wheel=new CarWheel("my carWheel");
                Thread.sleep(2000);
                log.info("made wheel success");
                return wheel;
            });
    
    
            carBodyFutureTask.run();
            carWheelFutureTask.run();
    
            CarBody carBody=carBodyFutureTask.get();
            CarWheel wheel=carWheelFutureTask.get();
    
            //制造车子
            Car car=new Car(carBody,wheel);
    
            //从结果看出来,是同步执行的
            log.info(car.toString());
    
        }
    
        @Test
        public void madeCarUseFuturePool() throws ExecutionException, InterruptedException {
    
            ThreadPoolExecutor executor=new ThreadPoolExecutor(20,50,3, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(200),new ThreadPoolExecutor.DiscardOldestPolicy());
    
            //制造body
    
            FutureTask<CarBody> carBodyFutureTask=new FutureTask<CarBody>(new Callable<CarBody>() {
                @Override
                public CarBody call() throws Exception {
                    log.info("madde body start");
                    CarBody body=new CarBody("my carbody");
                    Thread.sleep(2000);
                    log.info("madde body success");
                    return body;
                }
            });
    
    
            //制造轮子
            FutureTask<CarWheel> carWheelFutureTask=new FutureTask<CarWheel>(()->{
                log.info("made wheel start");
                CarWheel wheel=new CarWheel("my carWheel");
                Thread.sleep(2000);
                log.info("made wheel success");
                return wheel;
            });
            executor.submit(carBodyFutureTask);
            executor.submit(carWheelFutureTask);
    
    
            CarBody carBody=carBodyFutureTask.get();
            CarWheel wheel=carWheelFutureTask.get();
    
            //制造车子
            Car car=new Car(carBody,wheel);
    
            //从结果看出来,是异步执行的
            log.info(car.toString());
    
        }
    

    下一章 java并发编程 - 9 - ThreadlLocal 避免共享

    相关文章

      网友评论

          本文标题:java并发编程 - 8 - Future

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