首页 > 编程知识 正文

spring boot线程池使用例子,springboot线程池使用

时间:2023-05-06 20:44:18 阅读:198786 作者:633

package com.wdwl.changfa.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;@Configuration@EnableAsyncpublic class OneTaskPoolConfig { @Bean("oneTaskExecutor") public Executor taskExecutro(){ ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); //线程池维护线程的最少数量 taskExecutor.setCorePoolSize(10); //线程池维护线程的最大数量 taskExecutor.setMaxPoolSize(100); //线程池所使用的缓冲队列 taskExecutor.setQueueCapacity(10000); //线程池维护线程所允许的空闲时间 taskExecutor.setKeepAliveSeconds(60); taskExecutor.setAllowCoreThreadTimeOut(true); taskExecutor.setThreadNamePrefix("oneTaskExecutor--"); taskExecutor.setWaitForTasksToCompleteOnShutdown(true); taskExecutor.setAwaitTerminationSeconds(60); return taskExecutor; }} package com.wdwl.changfa.service.impl;import com.alibaba.fastjson.JSON;import com.wdwl.changfa.mapper.TbStudentMapper;import com.wdwl.changfa.model.TbStudent;import com.wdwl.changfa.service.IAsyncTaskService;import lombok.extern.slf4j.Slf4j;import org.jsoup.Connection;import org.jsoup.Jsoup;import org.jsoup.nodes.Element;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;import java.io.IOException;@Service@Slf4jpublic class AsyncTaskServiceImpl implements IAsyncTaskService { @Autowired private TbStudentMapper studentMapper; @Override @Async("oneTaskExecutor") public void start(TbStudent student) throws IOException { log.debug(Thread.currentThread().getName()); }}

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。