【自用记录】个人线程池套件模板

存:


/**
   线程池
 * @author zx
 * @date 2019年05月13日
 */
@Service
public class Demo {

    private static final Logger logger = LoggerFactory.getLogger(Demo.class);

    /**
     * 下载失败最大重试次数
     */
    private static final int MAX_RETRY_COUNT = 3;


    /**
     * 下载描述存储的最大长度.
     */
    private static final int MAX_DESCRIBE_LENGTH = 100;
    /**
     * 线程池参数
     */
    private static final int FILE_MIN = 5 * 60 * 1000;
    private static final int ONE_HOUR = 1 * 60 * 60 * 1000;
    private static final long KEEP_ALIVE_TIME = 500L;
    @Switch(name = "TABLE_JOB-corePoolSize", description = "核心线程数量配置")
    private int corePoolSize = 10;
    @Switch(name = "TABLE_JOB-maxPoolSize", description = "最大线程数量配置")
    private int maxPoolSize = 20;
    private LinkedBlockingQueue workQueen = new LinkedBlockingQueue<>();






    /**
     * This ExecutorService  will create a new thread pool object  with the given params
     */
    private ThreadPoolExecutor executor ;
    /**
     * init thread pool
     */
    private ThreadPoolExecutor initThreadPool() {
        return new ThreadPoolExecutor(
            // core pool size
            corePoolSize,
            // max pool size
            maxPoolSize,
            // keep alive time
            KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
            // queue
            workQueen,
            // handler
            new ThreadPoolExecutor.CallerRunsPolicy());
    }
    /**
     * 等待任务结束并关闭资源
     * @param executor    线程池
     */

    private void closePool(ThreadPoolExecutor executor) {
        //等待执行结束返回
        int waitTime = 0;
        while (executor.getActiveCount() != 0) {
            try {
                Thread.sleep(FILE_MIN);
                waitTime += FILE_MIN;
                logger.warn("MEMBER TASK RUNNING...left task= " + workQueen.size());
                if (waitTime >= ONE_HOUR) {
                    logger.warn("MEMBER TASK INTERRUPTED");
                    executor.shutdownNow();
                    break;
                }
            } catch (InterruptedException e) {
                logger.error("MEMBER TASK InterruptedException",e);
            }
        }
        //关闭资源
        if(executor!=null){
            executor.shutdownNow();
        }
    }

    
    private void parseFile(List<TaskA> taskList) {
        executor=initThreadPool();
        for (TaskA task : taskList) {
            UpdateLevelThread childTask = new UpdateLevelThread(task.getName());
            Future<String> result = executor.submit(childTask);
            //String returnResult=result.get();
        }
        closePool(executor);
        logger.warn("END PARSE FILE >");
    }




}

class UpdateLevelThread implements Callable<String> {
    private static final String VENDOR_MARRIOTT = "MRT";

    private String param;


    @Override
    public String call() {
        return updateLevel();
    }
    public UpdateLevelThread(String param
    }
    /**
     * 更新信息

     * @return error message
     */
    private synchronized String updateLevel() {
        return "success";
    }
}
原文地址:https://www.cnblogs.com/the-fool/p/11054050.html