zoukankan      html  css  js  c++  java
  • 【自用记录】个人线程池套件模板

    存:

    
    /**
       线程池
     * @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";
        }
    }
  • 相关阅读:
    使用C#中的DirectorySearcher来获得活动目录中的组织结构与用户等信息,并在展示成树形结构(附源代码)
    oracle的简单操作和要注意的地方
    lambda匿名函数
    Linux查看系统信息(版本、cpu、raid)
    chmod 777后,目录权限不可写解决方法
    linux /boot空间满了如何清理
    k3s
    IDEA项目编译参数Werror设置
    minicube 安装
    ubuntu安装docker
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054050.html
Copyright © 2011-2022 走看看