【自用记录】个人线程池套件模板-已过时

旧博文有一定啊的参考意义

缺点:首先这个自定义的等待器由于使用了唯一的队列,所以每个新任务需要创建一个池子。

优点:可以查看进度

目前博主使用的方式:

https://blog.csdn.net/the_fool_/article/details/103144826

=========================================================================

存:


/**
   线程池
 * @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";
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值