1 2 3 4

线程池的使用

线程池的使用

1.配置文件配置线程池数量等:application.properties

#线程池配置
ud.thread.pool.corePoolSize=20
ud.thread.pool.maximumPoolSize=50
ud.thread.pool.keepAliveTime=30
ud.thread.pool.blockQueue=30

2.初始化线程池,随容器的启动而启动

package cn.cjq.bomb.util;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池初始化(推荐)
 * 初始化的线程池无需关闭
 * @author cjq
 */
public class BlockPoolExecutor {

    /**
     * 日志
     */
    private static final Logger logger = LogManager.getLogger(BlockPoolExecutor.class);
    /**
     * 核心线程池大小
     */
    private static int corePoolSize;
    /**
     * 最大线程池大小
     */
    private static int maximumPoolSize;
    /**
     * 线程池中超过corePoolSize数目的空闲线程的最大存活时间,单位根据TimeUnit
     */
    private static long keepAliveTime;
    /**
     * 阻塞队列大小
     */
    private static int blockQueue;
    /**
     * 用户自定义线程池
     */
    private static ThreadPoolExecutor udPool = null;

    static {
        InputStream in = null;
        in = BlockPoolExecutor.class.getClassLoader().getResourceAsStream("application.properties");
        Properties properties = new Properties();
        try {
            properties.load(in);
            String udCorePoolSize = properties.getProperty("ud.thread.pool.corePoolSize", "30");
            String udMaximumPoolSize = properties.getProperty("ud.thread.pool.maximumPoolSize", "50");
            String udKeepAliveTime = properties.getProperty("ud.thread.pool.keepAliveTime", "30");
            String udBlockQueue = properties.getProperty("ud.thread.pool.blockQueue", "30");
            if (udCorePoolSize != null && udCorePoolSize.trim().length() > 0) {
                corePoolSize = Integer.valueOf(udCorePoolSize);
            }
            if (udMaximumPoolSize != null && udMaximumPoolSize.trim().length() > 0) {
                maximumPoolSize = Integer.valueOf(udMaximumPoolSize);
            }
            if (udKeepAliveTime != null && udKeepAliveTime.trim().length() > 0) {
                keepAliveTime = Integer.valueOf(udKeepAliveTime);
            }
            if (udBlockQueue != null && udBlockQueue.trim().length() > 0) {
                blockQueue = Integer.valueOf(udBlockQueue);
            }

        } catch (IOException e) {
            e.printStackTrace();
            logger.error("BlockPoolExecutor.static.IOException 初始化线程池失败:" + e.getMessage());
        }
        udPool = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<Runnable>(blockQueue)
        );
    }

    /**
     * 关闭线程池
     */
    public static void destroy() {
        if (udPool != null) {
            udPool.shutdown();
        }
    }

    /**
     * 获取线程池
     *
     * @return
     */
    public static ExecutorService getThreadPool() {
        return udPool;
    }


}

3.线程实例:

@ResponseBody
    @RequestMapping(value = "/blockThread", method = RequestMethod.GET)
    public String blockThread() {
        logger.info("开始获取线程池-------------------------------------------");
        ExecutorService executorService = BlockPoolExecutor.getThreadPool();
        try {
            for (int i = 0; i < 30; i++) {
                executorService.execute(new BatchThread(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("线程池运行异常-------------------------------------------");
        }
        return "线程池跑批成功";
    }

4.线程类:

package cn.cjq.bomb.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BatchThread implements Runnable {

    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(BatchThread.class);
    /**
     * 多线程当前进程数
     */
    private int num;

    public BatchThread(int num){
        this.num=num;
    }

    @Override
    public void run(){
        logger.info(num+"多线程跑批中");
    }
}

 

posted @ 2019-07-17 11:12  一缕清风丶  阅读(203)  评论(0编辑  收藏  举报