zoukankan      html  css  js  c++  java
  • 线程管理类

    package com.liuwei.comment;
    
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.Callable;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Future;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadManager {
    
        private static ThreadManager threadManager;
        private ThreadPoolExecutor threadPoolExecutor;
    
        public ThreadManager(int maxThreadCount) {
            threadPoolExecutor = new ThreadPoolExecutor(1, maxThreadCount, 200L,
                    TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
        }
    
        public synchronized static ThreadPoolExecutor getThreadManager(int maxThreadCount) {
            if (threadManager == null) {
                synchronized (ThreadManager.class) {
                    if (threadManager == null) {
                        threadManager = new ThreadManager(maxThreadCount);
                    }
                }
            }
            return threadManager.threadPoolExecutor;
        }
        
        public synchronized static ThreadManager getThreadManager() {
            return threadManager;
        }
        // 执行任务,当线程池处于关闭,将会重新创建的线程池
        public synchronized void execute(Runnable runn) {
            if (runn == null) {
                return;
            }
            if (threadPoolExecutor == null || threadPoolExecutor.isShutdown()) {
                threadPoolExecutor = getThreadManager(1);
            }
            threadPoolExecutor.submit(runn);
        }
    
        // 执行任务,当线程池处于关闭,将会重新创建的线程池
        public synchronized <T> Future<T> execute(Callable<T> callable) {
            if (callable == null) {
                return null;
            }
            if (threadPoolExecutor == null || threadPoolExecutor.isShutdown()) {
                threadPoolExecutor = getThreadManager(1);
            }
            Future<T> future = threadPoolExecutor.submit(callable);
    
            return future;
        }
    
        /** 取消线程池中某个还未执行的任务 */
        public synchronized void cancel(Runnable runn) {
            if (threadPoolExecutor != null && !threadPoolExecutor.isShutdown()
                    || threadPoolExecutor.isTerminating()) {
                threadPoolExecutor.getQueue().remove(runn);
            }
        }
    
        /** 取消线程池中某个还未执行的任务 */
        public synchronized void cancel(Callable<?> callable) {
            if (threadPoolExecutor != null && !threadPoolExecutor.isShutdown()
                    || threadPoolExecutor.isTerminating()) {
                threadPoolExecutor.getQueue().remove(callable);
            }
        }
    
        /** 取消线程池中某个还未执行的任务 */
        public synchronized boolean contains(Callable<?> callable) {
            if (threadPoolExecutor != null
                    && (!threadPoolExecutor.isShutdown() || threadPoolExecutor.isTerminating())) {
                return threadPoolExecutor.getQueue().contains(callable);
            } else {
                return false;
            }
        }
    
        /** 取消线程池中某个还未执行的任务 */
        public synchronized boolean contains(Runnable runn) {
            if (threadPoolExecutor != null
                    && !threadPoolExecutor.isShutdown() || threadPoolExecutor.isTerminating()) {
                return threadPoolExecutor.getQueue().contains(runn);
            } else {
                return false;
            }
        }
    
        /** 立刻关闭线程池,并且正在执行的任务也将会被中断 */
        public void stop() {
            if (threadPoolExecutor != null
                    && !threadPoolExecutor.isShutdown() || threadPoolExecutor.isTerminating()) {
                 threadPoolExecutor.shutdown();
            }
        }
    
        /** 平缓关闭单任务线程池,但是会确保所有已经加入的任务都将会被执行完毕才关闭 */
        public synchronized void shutdown() {
            if (threadPoolExecutor != null
                    && !threadPoolExecutor.isShutdown() || threadPoolExecutor.isTerminating()) {
                threadPoolExecutor.shutdownNow();
            }
        }
    }
  • 相关阅读:
    [Sql Server][原创]
    SQL Server T-SQL高级查询
    SQL 网文链接
    Epicor系统二次开发
    lambda表达式的变量作用域
    写一个正则表达式匹配手机号
    函数装饰器在类方法中的使用方法
    关于Django的session的使用 (装饰器版)
    Django ORM相关操作(2)
    Django ORM相关操作(1)
  • 原文地址:https://www.cnblogs.com/krislight1105/p/11437972.html
Copyright © 2011-2022 走看看