spring通过一个注解关键字 @Async 表明一段代码将放入异步线程代码块里面运行。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
@Service
public class MyService {
private static final Logger logger = LoggerFactory.getLogger(MyService.class);
public MyService() {
}
//这部分代码将放入到一个线程内完成。
@Async
public CompletableFuture<Integer> sum(int a, int b) throws InterruptedException {
logger.info("计算: " + a + " + " + b);
int s = a + b;
Thread.sleep(3 * 1000);
logger.info("计算: " + a + " + " + b + " 结束");
return CompletableFuture.completedFuture(s);
}
}
上面代码示例计算两数之和,假设很耗时,所以放到线程里面执行。
定义一个runner:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;
@Component
public class MyAppRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(MyAppRunner.class);
private final MyService myService;
public MyAppRunner(MyService service) {
this.myService = service;
}
@Override
public void run(String... args) throws Exception {
logger.info("-" + System.currentTimeMillis());
CompletableFuture s1 = myService.sum(1, 2);
CompletableFuture s2 = myService.sum(3, 4);
CompletableFuture s3 = myService.sum(5, 6);
CompletableFuture s4 = myService.sum(7, 8);
//CompletableFuture.allOf(s1, s2, s3, s4).join();
logger.info("--" + System.currentTimeMillis());
logger.info("1,2--> " + s1.get());
logger.info("3,4--> " + s2.get());
logger.info("5,6--> " + s3.get());
logger.info("7,8--> " + s4.get());
}
}
在spring的application里面关闭main()函数里面的线程池,取而代之由自己定义的一个线程池执行线程任务:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@SpringBootApplication
@EnableAsync
public class SpringAsyncApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAsyncApplication.class, args).close();
}
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("phil-thread:");
executor.initialize();
return executor;
}
}
运行日志输出(截取部分):
:02:59.815 INFO 37044 --- [ main] c.e.spring_async.SpringAsyncApplication : Started SpringAsyncApplication in 1.556 seconds (JVM running for 2.012)
:02:59.817 INFO 37044 --- [ main] com.example.spring_async.MyAppRunner : -1642158179816
:02:59.821 INFO 37044 --- [ main] com.example.spring_async.MyAppRunner : --1642158179820
:02:59.827 INFO 37044 --- [ phil-thread:1] com.example.spring_async.MyService : 计算: 1 + 2
:02:59.827 INFO 37044 --- [ phil-thread:3] com.example.spring_async.MyService : 计算: 5 + 6
:02:59.827 INFO 37044 --- [ phil-thread:2] com.example.spring_async.MyService : 计算: 3 + 4
:02:59.827 INFO 37044 --- [ phil-thread:4] com.example.spring_async.MyService : 计算: 7 + 8
:03:02.832 INFO 37044 --- [ phil-thread:2] com.example.spring_async.MyService : 计算: 3 + 4 结束
:03:02.832 INFO 37044 --- [ phil-thread:4] com.example.spring_async.MyService : 计算: 7 + 8 结束
:03:02.832 INFO 37044 --- [ phil-thread:1] com.example.spring_async.MyService : 计算: 1 + 2 结束
:03:02.832 INFO 37044 --- [ phil-thread:3] com.example.spring_async.MyService : 计算: 5 + 6 结束
:03:02.832 INFO 37044 --- [ main] com.example.spring_async.MyAppRunner : 1,2--> 3
:03:02.832 INFO 37044 --- [ main] com.example.spring_async.MyAppRunner : 3,4--> 7
:03:02.832 INFO 37044 --- [ main] com.example.spring_async.MyAppRunner : 5,6--> 11
:03:02.832 INFO 37044 --- [ main] com.example.spring_async.MyAppRunner : 7,8--> 15
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://zhangphil.blog.csdn.net/article/details/122500396
内容来源于网络,如有侵权,请联系作者删除!