zoukankan      html  css  js  c++  java
  • 利用线程异步调用

    定义线程类

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    import java.util.ArrayList;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadFixedPool {
    
        private static final Logger logger = LogManager.getLogger(ThreadFixedPool.class);
    
        private static final int CORE_POOL_SIZE = 4;
        private static final int MAX_POOL_SIZE = 200;
    
        private static ExecutorService executorService;
        ArrayList<Runnable> runnables = new ArrayList<>();
    
        public ThreadFixedPool build() {
            if (null == executorService) {
                logger.debug("加载默认线程池,初始化线程数为:{}", CORE_POOL_SIZE);
                executorService = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, 0L, TimeUnit.MILLISECONDS,
                        new LinkedBlockingQueue<Runnable>(1024), new AipcThreadFactory(),
                        new ThreadPoolExecutor.AbortPolicy());
            }
            return this;
        }
    
        public ThreadFixedPool build(int poolSize) {
            if (null == executorService) {
                if (poolSize == 0) {
                    logger.warn("线程池异常 poolSize!");
                }
                if (poolSize < 1 || poolSize >= MAX_POOL_SIZE) {
                    logger.warn("线程数不合法");
                }
                logger.debug("加载自定义线程池,初始化线程数为:{}", poolSize);
                executorService = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, 0L, TimeUnit.MILLISECONDS,
                        new LinkedBlockingQueue<Runnable>(1024), new AipcThreadFactory(),
                        new ThreadPoolExecutor.AbortPolicy());
            }
            return this;
        }
    
        public void execute(Runnable runnable) {
            executorService.execute(runnable);
        }
    
        public void shutdown() {
            if (runnables.size() > 0) {
                for (Runnable r : runnables) {
                    executorService.execute(r);
                }
            }
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                logger.error("线程池shutdown 休眠异常", e);
            }
            executorService.shutdown();
        }
    }
    

     方法体进行调用

    new ThreadFixedPool().build().execute(new getProduct(products, product.getCustomerId()));

      异步方法体进行处理

    private class getProduct extends Thread {
            List<ProductListRsp> listRsp;
            String customerId = "";
    
            getProduct(List<ProductListRsp> listRsp, String customerId) {
                this.listRsp = listRsp;
                this.customerId = customerId;
            }
    
            @Override
            public void run() {
                for (ProductListRsp productListRsp : listRsp) {
                    ChangeVo vo = new ChangeVo();
                    vo.setAmt(1);
                    vo.setCustomerId(customerId);
                    vo.setId(productListRsp.getId());
                    nearService.getProduct(vo);
                }
            }
        }
    

      

  • 相关阅读:
    MKMapVIew学习系列2 在地图上绘制出你运行的轨迹
    WPF SDK研究 Intro(6) WordGame1
    WPF SDK研究 Intro(3) QuickStart3
    WPF SDK研究 Layout(1) Grid
    WPF SDK研究 目录 前言
    WPF SDK研究 Intro(7) WordGame2
    WPF SDK研究 Layout(2) GridComplex
    对vs2005创建的WPF模板分析
    WPF SDK研究 Intro(4) QuickStart4
    《Programming WPF》翻译 第6章 资源
  • 原文地址:https://www.cnblogs.com/lingduqianli/p/12743654.html
Copyright © 2011-2022 走看看