利用线程异步调用

2021-02-10 10:18

阅读:335

标签:exception   异步调用   evo   near   str   pre   vat   locking   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 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(1024), new AipcThreadFactory(),
                    new ThreadPoolExecutor.AbortPolicy());
        }
        return this;
    }

    public ThreadFixedPool build(int poolSize) {
        if (null == executorService) {
            if (poolSize == 0) {
                logger.warn("线程池异常 poolSize!");
            }
            if (poolSize = MAX_POOL_SIZE) {
                logger.warn("线程数不合法");
            }
            logger.debug("加载自定义线程池,初始化线程数为:{}", poolSize);
            executorService = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, 0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue(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 listRsp;
        String customerId = "";

        getProduct(List 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);
            }
        }
    }

  

 

利用线程异步调用

标签:exception   异步调用   evo   near   str   pre   vat   locking   java   

原文地址:https://www.cnblogs.com/lingduqianli/p/12743654.html


评论


亲,登录后才可以留言!