当前位置: 代码迷 >> 综合 >> Java 线程池(ThreadPoolExecutor)实现
  详细解决方案

Java 线程池(ThreadPoolExecutor)实现

热度:38   发布时间:2024-01-26 02:40:53.0

开发中,避免各种各样的实现,统一线程池实现


 


import lombok.extern.slf4j.Slf4j;import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;@Slf4j
public class DefaultThreadPoolExecutor {private ThreadPoolExecutor pool = null;public DefaultThreadPoolExecutor() {this(1, 10, 30, 100);}/*** 初始化线程** @param corePoolSize    核心线程池大小,最小数* @param maximumPoolSize 最大线程池大小* @param keepAliveTime   线程池中超过corePoolSize数目的空闲线程最大存活时间,单位分钟* @param queueCapacity   队列容量*/public DefaultThreadPoolExecutor(Integer corePoolSize, Integer maximumPoolSize, Integer keepAliveTime, Integer queueCapacity) {pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(queueCapacity), new DefaultThreadFactory(), new DefaultRejectedExecutionHandler());log.info("初始化线程池:{corePoolSize:{},maximumPoolSize:{},keepAliveTime:{},queueCapacity:{}}", corePoolSize, maximumPoolSize, keepAliveTime, queueCapacity);}public void destory() {if (pool != null) {pool.shutdownNow();}}public ExecutorService getDefaultThreadPoolExecutor() {return this.pool;}private class DefaultRejectedExecutionHandler implements RejectedExecutionHandler {@Overridepublic void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {try {CommonTask commonTask = (CommonTask) runnable;log.info("线程池添加任务{}被拒绝,触发拒绝策略", commonTask.getTaskName());//todo  Inserts the specified element into this queue, waiting if necessary//     for space to become available.executor.getQueue().put(runnable);} catch (InterruptedException e) {log.error("在堵塞等待方式添加任务到线程时获得异常", e);}}}/*** 线程工厂,指定线程名*/private class DefaultThreadFactory implements ThreadFactory {private AtomicInteger count = new AtomicInteger(0);@Overridepublic Thread newThread(Runnable runnable) {Thread thread = new Thread(runnable);String threadName = DefaultThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);thread.setName(threadName);return thread;}}
}

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;/**** @author linxinqiang@126.com* @date 2018年5月18日 下午2:53:23*/
@Configuration
@ConditionalOnProperty(name = "thread.active", havingValue = "true")
@Slf4j
public class ThreadPoolManager {@Value("${thread.pool.coreSize}")private Integer poolCoreSize;@Value("${thread.pool.maxSize}")private Integer poolMaxSize;@Value("${thread.pool.keepAliveTime}")private Integer poolKeepAliveTime;@Value("${thread.queue.capacity}")private Integer queueCapacity;private static ThreadPoolManager instance;private DefaultThreadPoolExecutor threadPoolExecutor;@PostConstructpublic void init() {instance = this;}private DefaultThreadPoolExecutor getThreadPoolExecutor() {if (threadPoolExecutor == null) {synchronized (this) {threadPoolExecutor = new DefaultThreadPoolExecutor(poolCoreSize, poolMaxSize, poolKeepAliveTime, queueCapacity);}}return threadPoolExecutor;}public static ThreadPoolManager getInstance() {return instance;}public void submit(CommonTask task) {if (task != null) {if (log.isDebugEnabled()) {log.debug("新线程池任务:{}", task.getTaskName());}this.getThreadPoolExecutor().getDefaultThreadPoolExecutor().execute(task);}}
}
/*** 通用任务管理执行器父类接口** @author linxinqiang90@126.com* @date 2018年6月4日 上午10:45:45*/
public interface CommonTask extends Runnable {String getTaskName();
}

 

  相关解决方案