本文共 4875 字,大约阅读时间需要 16 分钟。
线程池是Java中一个核心的并发处理机制,主要用于高效管理和调度线程资源。它能够最大化地利用系统资源,同时避免因过度创建线程而引发的性能问题。Spring框架内置了丰富的线程池功能,允许开发者灵活配置和管理线程池,适用于不同的业务场景。
线程池的核心配置参数包括核心线程池大小 (corePoolSize)、最大线程池大小 (maxPoolSize)、任务队列容量 (queueCapacity)、线程空闲存活时间 (keepAliveSeconds)、任务等待终止时间 (awaitTerminationSeconds) 和线程名称前缀 (threadNamePrefix)。
线程池的逻辑分为以下几个方面:
corePoolSize 和 queueCapacity 在程序运行时是一个动态值。keepAliveSeconds 定时销毁。getActiveCount() 和 getPoolSize() 可以判断线程池是否有空闲线程。corePoolSize):建议根据业务需求设置合理的线程数,默认值为1。maxPoolSize):默认值为无限,建议根据硬件资源限制设置上限。queueCapacity):默认值为无限,建议根据任务吞吐量设置合理值。keepAliveSeconds):默认值为60秒,建议根据业务场景调整。awaitTerminationSeconds):默认值为180秒,建议根据业务需要调整。@Data@NoArgsConstructorpublic class ThreadPoolConfig { private int corePoolSize = 1; private int maxPoolSize = Integer.MAX_VALUE; private int keepAliveSeconds = 60; private int queueCapacity = Integer.MAX_VALUE; private int awaitTerminationSeconds = 180; private String threadNamePrefix = "ThreadPoolConfig"; private boolean allowCoreThreadTimeOut = true;} @Configurationpublic class ThreadPool { @Bean(name = "customDefaultThreadPool") public Executor customDefaultThreadPool() { ThreadPoolConfig config = new ThreadPoolConfig(10, 20, "customDefaultThreadPool"); return customThreadPool(config); } @Bean(name = "receiveOrderThreadPool") public Executor receiveOrderThreadPool() { ThreadPoolConfig config = new ThreadPoolConfig(20, 50, "receiveOrderThreadPool"); return customThreadPool(config); } @Bean(name = "handlerOrderThreadPool") public Executor handlerOrderThreadPool() { ThreadPoolConfig config = new ThreadPoolConfig(10, 20, "handlerOrderThreadPool"); return customThreadPool(config); } private Executor customThreadPool(ThreadPoolConfig config) { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(config.getCorePoolSize()); executor.setMaxPoolSize(config.getMaxPoolSize()); executor.setKeepAliveSeconds(config.getKeepAliveSeconds()); executor.setQueueCapacity(config.getQueueCapacity()); executor.setAllowCoreThreadTimeOut(config.isAllowCoreThreadTimeOut()); executor.setThreadNamePrefix(config.getThreadNamePrefix()); executor.setAwaitTerminationSeconds(config.getAwaitTerminationSeconds()); executor.setWaitForTasksToCompleteOnShutdown(Boolean.TRUE); executor.initialize(); return executor; }} @Componentpublic class OrderComponent { @Async("customDefaultThreadPool") public void testDefaultThreadPool(Integer i) { log.info("默认线程池-开始:索引-{}", i); try { Thread.sleep(1000L); log.info("默认线程池-结束:索引-{}", i); } catch (InterruptedException e) { log.error("睡眠线程异常:{}", e.toString()); } } @Async("receiveOrderThreadPool") public void testReceiveOrderThreadPool(Integer i) { log.info("接单线程池-开始:索引-{}", i); try { Thread.sleep(1000L); log.info("接单线程池-结束:索引-{}", i); } catch (InterruptedException e) { log.error("睡眠线程异常:{}", e.toString()); } } @Async("handlerOrderThreadPool") public void testHandlerOrderThreadPool(Integer i) { log.info("处理订单线程池-开始:索引-{}", i); try { Thread.sleep(1000L); log.info("处理订单线程池-结束:索引-{}", i); } catch (InterruptedException e) { log.error("睡眠线程异常:{}", e.toString()); } }} 通过 ThreadPoolTaskExecutor 可以动态修改线程池参数,例如:
@Autowired@Lazyprivate ThreadPoolTaskExecutor customDefaultThreadPool;public String testThreadPool(int forEachNum) { try { int activeCount = customDefaultThreadPool.getActiveCount(); int corePoolSize = customDefaultThreadPool.getPoolSize(); System.out.println("customDefault activeCount: " + activeCount); System.out.println("customDefault corePoolSize: " + corePoolSize); for (Integer i = 1; i <= forEachNum; i++) { try { orderComponent.testDefaultThreadPool(i); } catch (RejectedExecutionException e) { return "线程资源不够用, 拒绝执行任务,请稍后重试"; } } return "success"; } catch (Exception e) { return ""; }} corePoolSize、maxPoolSize 和 queueCapacity。keepAliveSeconds,避免资源浪费。awaitTerminationSeconds,避免线程资源耗尽。通过合理配置和使用Spring自定义线程池,可以有效地管理并发任务,优化系统性能。
转载地址:http://tbvfk.baihongyu.com/