https://blog.csdn.net/github_39936816/article/details/80557977
https://blog.csdn.net/lu_wei_wei/article/details/80242831
@Configuration public class ThreadPoolConfig {
//這些值也在配置文件配置 // 核心線程池大小 private int corePoolSize = 50; // 最大可創建的線程數 private int maxPoolSize = 200; // 隊列最大長度 private int queueCapacity = 1000; // 線程池維護線程所允許的空閑時間 private int keepAliveSeconds = 300; @Bean(name = "threadPoolTaskExecutor") public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setMaxPoolSize(maxPoolSize); executor.setCorePoolSize(corePoolSize); executor.setQueueCapacity(queueCapacity); executor.setKeepAliveSeconds(keepAliveSeconds); // 線程池對拒絕任務(無線程可用)的處理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); return executor; } }
調用
@Autowired
private ThreadPoolConfig threadPoolConfig;
方法中調用----->
threadPoolConfig.threadPoolTaskExecutor().execute(new Runnable() { @Override public void run() { try { String password = String.valueOf((int) ((Math.random() * 9 + 1) * 100000)); user.setSalt(ShiroUtils.randomSalt()); user.setPassword(passwordService.encryptPassword(loginName, password, user.getSalt())); if (userService.resetUserPwd(user) > 0) { String content = "********"; mailService.sendHtmlMail(user.getEmail(), "您的登錄密碼已經修改!", content); } } catch (Exception e) { System.out.println("郵件發送異常"); e.printStackTrace(); } } });