spring中的异步任务@Async
1.在springboot项目中,我们使用异步任务
2.推荐使用线程池维护异步任务,并给异步任务增加默认线程池
3.实现如下:
package com.alspd.wx.async;import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;@Component
@Slf4j
public class AsyncTask {@SneakyThrows@Asyncpublic void doTask1() {long t1 = System.currentTimeMillis();Thread.sleep(2000);long t2 = System.currentTimeMillis();log.info("task1 cost {} ms" , t2-t1);}@SneakyThrows@Asyncpublic void doTask2() {long t1 = System.currentTimeMillis();Thread.sleep(3000);long t2 = System.currentTimeMillis();log.info("task2 cost {} ms" , t2-t1);}
}
package com.alspd.wx.controller;import com.alspd.wx.async.AsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/async")
@Slf4j
public class AsyncController {@Autowiredprivate AsyncTask asyncTask;@GetMapping("/task")public void task() throws InterruptedException {long t1 = System.currentTimeMillis();asyncTask.doTask1();asyncTask.doTask2();Thread.sleep(1000);long t2 = System.currentTimeMillis();log.info("main cost {} ms", t2 - t1);}
}
异步配置
package com.alspd.wx.async;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;/*** 类描述:** @author zhang pan* @version 1.0* @date 2023/3/28 10:05*/@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {@Bean(name = "asyncPoolTaskExecutor")public ThreadPoolTaskExecutor executor() {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();//核心线程数taskExecutor.setCorePoolSize(10);//线程池维护线程的最大数量,只有在缓冲队列满了之后才会申请超过核心线程数的线程taskExecutor.setMaxPoolSize(100);//缓存队列taskExecutor.setQueueCapacity(50);//许的空闲时间,当超过了核心线程出之外的线程在空闲时间到达之后会被销毁taskExecutor.setKeepAliveSeconds(200);//异步方法内部线程名称taskExecutor.setThreadNamePrefix("async-");/*** 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize,如果还有任务到来就会采取任务拒绝策略* 通常有以下四种策略:* ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。* ThreadPoolExecutor.DiscardPolicy:也是丢弃任务,但是不抛出异常。* ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)* ThreadPoolExecutor.CallerRunsPolicy:重试添加当前的任务,自动重复调用 execute() 方法,直到成功*/taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());taskExecutor.initialize();return taskExecutor;}/*** 指定默认线程池*/@Overridepublic Executor getAsyncExecutor() {return executor();}
}
收官。
spring中的异步任务@Async
1.在springboot项目中,我们使用异步任务
2.推荐使用线程池维护异步任务,并给异步任务增加默认线程池
3.实现如下:
package com.alspd.wx.async;import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;@Component
@Slf4j
public class AsyncTask {@SneakyThrows@Asyncpublic void doTask1() {long t1 = System.currentTimeMillis();Thread.sleep(2000);long t2 = System.currentTimeMillis();log.info("task1 cost {} ms" , t2-t1);}@SneakyThrows@Asyncpublic void doTask2() {long t1 = System.currentTimeMillis();Thread.sleep(3000);long t2 = System.currentTimeMillis();log.info("task2 cost {} ms" , t2-t1);}
}
package com.alspd.wx.controller;import com.alspd.wx.async.AsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/async")
@Slf4j
public class AsyncController {@Autowiredprivate AsyncTask asyncTask;@GetMapping("/task")public void task() throws InterruptedException {long t1 = System.currentTimeMillis();asyncTask.doTask1();asyncTask.doTask2();Thread.sleep(1000);long t2 = System.currentTimeMillis();log.info("main cost {} ms", t2 - t1);}
}
异步配置
package com.alspd.wx.async;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;/*** 类描述:** @author zhang pan* @version 1.0* @date 2023/3/28 10:05*/@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {@Bean(name = "asyncPoolTaskExecutor")public ThreadPoolTaskExecutor executor() {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();//核心线程数taskExecutor.setCorePoolSize(10);//线程池维护线程的最大数量,只有在缓冲队列满了之后才会申请超过核心线程数的线程taskExecutor.setMaxPoolSize(100);//缓存队列taskExecutor.setQueueCapacity(50);//许的空闲时间,当超过了核心线程出之外的线程在空闲时间到达之后会被销毁taskExecutor.setKeepAliveSeconds(200);//异步方法内部线程名称taskExecutor.setThreadNamePrefix("async-");/*** 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize,如果还有任务到来就会采取任务拒绝策略* 通常有以下四种策略:* ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。* ThreadPoolExecutor.DiscardPolicy:也是丢弃任务,但是不抛出异常。* ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)* ThreadPoolExecutor.CallerRunsPolicy:重试添加当前的任务,自动重复调用 execute() 方法,直到成功*/taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());taskExecutor.initialize();return taskExecutor;}/*** 指定默认线程池*/@Overridepublic Executor getAsyncExecutor() {return executor();}
}
收官。
发布评论