美文网首页
线程池(1)

线程池(1)

作者: YNZXGWZM | 来源:发表于2018-08-02 14:55 被阅读0次

ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强。我们知道Future表示一个异步计算任务,当任务完成时可以得到计算结果。如果我们希望一旦计算完成就拿到结果展示给用户或者做另外的计算,就必须使用另一个线程不断的查询计算状态。这样做,代码复杂,而且效率低下。使用ListenableFuture Guava帮我们检测Future是否完成了,如果完成就自动调用回调函数,这样可以减少并发程序的复杂度。

package com.mc.ThredPool;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

public class ThredPoolTest implements GuavaInteface {
    private static ListeningExecutorService pool;

    static {
        //通过guava创建固定容量的线程池,用完需要调用shutdown方法关闭线程池。
        pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));

    }

    @Test
    public void test() throws ExecutionException, InterruptedException {
        Instant now = Instant.now();
        Student st = new Student("付萌1", "女", 12, "高新");
        Student st2 = new Student("付萌1", "女", 13, "高新");
        Student st3 = new Student("付萌1", "女", 14, "高新");
        Student st4 = new Student("付萌1", "女", 15, "高新");
        Person p = new Person();
        copyDto(p, st);
        List result1 = new ArrayList<Student>();
        List result2 = new ArrayList<Person>();
        ListenableFuture<List> submit = pool.submit(() -> { //这里使用了lambda表达式,
            for (int i = 0; i < 100000000; i++) { //也可以直接通过匿名内部类实现callable,runnable区别,一个有返回值,一个没有返回值
                result1.add(st);
            }
            return result1;
        });
        ListenableFuture<List> submit1 = pool.submit(() -> {
            for (int i = 0; i < 100000000; i++) {
                result2.add(p);
            }
            return result2;
        });
        List list1 = submit.get();
        List list = submit1.get();
        pool.shutdown();//用完之后关闭线程池
        Instant now1 = Instant.now();
        System.out.println(list.size());
        System.out.println(list1.size());
        System.out.println("使用线程池耗时:"+Duration.between(now, now1).toMillis());
    }
    @Test
    public void test1() throws ExecutionException, InterruptedException {
        Instant now = Instant.now();
        Student st = new Student("付萌1", "女", 12, "高新");
        Student st2 = new Student("付萌1", "女", 13, "高新");
        Student st3 = new Student("付萌1", "女", 14, "高新");
        Student st4 = new Student("付萌1", "女", 15, "高新");
        Person p = new Person();
        copyDto(p, st);
        List result1 = new ArrayList<Student>();
        List result2 = new ArrayList<Person>();
        for (int i = 0; i < 100000000; i++) {
            result1.add(st);
        }
        for (int i = 0; i < 100000000; i++) {
            result2.add(p);
        }

        Instant now1 = Instant.now();
        System.out.println(result1.size());
        System.out.println(result2.size());
        System.out.println("不使用线程池耗时:"+Duration.between(now, now1).toMillis());
    }
}

实际开发中,要是遇到多个查询(查询结果数据量很大的情况下)的话,可以考虑使用线程池,每个线程跑一个查询,增加效率,


1533196163(1).jpg

实际业务使用场景

    ListenableFuture<List<WaybillServiceFeeVo>> futureTask = pool.submit(() -> {
            List<WaybillServiceFeeVo> feeList = feeService.queryFeeAndRow2ColumnByWaybillNos(waybillNoArray);
            return feeList;
        });

        ListenableFuture<List<ReceiveAddress>> addressFuture = pool.submit(new Callable<List<ReceiveAddress>>() {
            @Override
            public List<ReceiveAddress> call() throws Exception {
                List<ReceiveAddress> addrList = addressService.queryAddressByWaybillNos(waybillNoArray);
                return addrList;
            }
        });

        ListenableFuture<List<WaybillApply>> applyFuture = pool.submit(new Callable<List<WaybillApply>>() {
            @Override
            public List<WaybillApply> call() throws Exception {
                List<WaybillApply> applyList = waybillApplyService.queryApplyByNos(waybillNoArray);
                return applyList;
            }
        });

        try {
            List<WaybillServiceFeeVo> feeList = futureTask.get();
            if (feeList != null) {
                batchCascadeWaybillFee(dtoList, feeList);
            }
            List<ReceiveAddress> addrList = addressFuture.get();
            if (addrList != null) {
                batchCascadeWaybillAddress(dtoList, addrList);
            }
            List<WaybillApply> applyList = applyFuture.get();
            if (applyList != null) {
                applyConvertWaybillDto(dtoList, applyList);
            }
        } catch (InterruptedException | ExecutionException e) {
            throw new BusinessRuntimeException("等待失败");
        }
        return dtoList;
    }

相关文章

  • 线程以及java线程池实现分享

    线程以及java线程池实现分享 线程简介 JDK线程池的工作原理 JDK线程池的实现细节 1.线程简介-由来 1....

  • Android 线程池原理

    线程池核心类 : ThreadPoolExecutor:提供了一系列参数来配置线程池 线程池优点: 1.重用线程池...

  • 线程池

    1.线程池简介 1.1 线程池的概念 线程池就是首先创建一些线程,它们的集合称为线程池。使用线程池可以很好地提高性...

  • 面试题2019年7月

    线程池原理 参考:Java 线程池原理分析 线程池工作原理:1、线程数量小于 corePoolSize,直接创建新...

  • 线程池

    线程池组件 1、线程池管理器(ThreadPoolManager):用于创建并管理线程池 2、工作线程(WorkT...

  • 线程池相关知识

    线程池 1. 什么是线程池 线程的池化,一个线程的容器、集合,包含多个线程 2. 为什么要用线程池 线程对于操作系...

  • 线程池核心参数

    线程池核心参数 1)corePoolSize(线程池基本大小) 2)maximumPoolSize(线程池最大数量...

  • 线程池学习笔记

    1、线程池的定义 2、Executors创建线程池的方式 3、ThreadPoolExecutor对象 4、线程池...

  • 线程池(3)终止线程池原理

    终止线程池 一、终止线程池方法 1、 shutdown() 安全的终止线程池 2、 shutdownNow() 强...

  • 线程池

    线程池 a. 基本组成部分: 1、线程池管理器(ThreadPool):用于创建并管理线程池,包括创建线程池,销毁...

网友评论

      本文标题:线程池(1)

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