在我们的日常开发中,我们偶尔会遇到在业务层中我们需要同时修改多张表的数据并且需要有序的执行,如果我们用往常的同步的方式,也就是单线程的方式来执行的话,可能会出现执行超时等异常造成请求结果失败,及时成功,前端也需要等待较长时间来获取响应结果,这样不但造成了用户体验差,而且会经常出现请求执行失败的问题,在这里我们一般会采用3种方式来处理,如下所示:
Controller
package com.carlo.test.spboottest.controller;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.carlo.test.spboottest.service.TestAsyncService;
import com.carlo.test.spboottest.service.TestAsyncService2;
@RestController
public class TestController {
@Autowired
private TestAsyncService testAsyncService;
@Autowired
private TestAsyncService2 testAsyncService2;
/**
* 使用传统方式测试
*/
@GetMapping("/test")
public void test() {
System.out.println("获取主线程名称:" + Thread.currentThread().getName());
testAsyncService.serviceTest();
System.out.println("执行成功,返回结果");
}
/**
* 使用传统方式测试
*/
@GetMapping("/test1")
public void test1() {
System.out.println("获取主线程名称:" + Thread.currentThread().getName());
/**
* 这里也可以采用以下方式使用,但是使用线程池的方式可以很便捷的对线程管理(提高程序的整体性能),
* 也可以减少每次执行该请求时都需要创建一个线程造成的性能消耗
* new Thread(() ->{
* run方法中的业务逻辑
* })
*/
/**
* 定义一个线程池
* 核心线程数(corePoolSize):1
* 最大线程数(maximumPoolSize): 1
* 保持连接时间(keepAliveTime):50000
* 时间单位 (TimeUnit):TimeUnit.MILLISECONDS(毫秒)
* 阻塞队列 new LinkedBlockingQueue<Runnable>()
*/
ThreadPoolExecutor executor = new ThreadPoolExecutor(1,5,50000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
// 执行业务逻辑方法serviceTest()
executor.execute(new Runnable() {
@Override
public void run() {
testAsyncService.serviceTest();
}
});
System.out.println("执行完成,向用户响应成功信息");
}
/**
* 使用@Async来实现
*/
@GetMapping("/test3")
public void test3() {
System.out.println("获取主线程名称:" + Thread.currentThread().getName());
// 异步调用
testAsyncService2.serviceTest();
System.out.println("执行完成,向用户响应成功信息");
}
}
SERVICE1
public interface TestAsyncService { public void serviceTest(); }
SERVICEIMPL1
import java.util.Arrays;
import org.springframework.stereotype.Service;
import com.carlo.test.spboottest.service.TestAsyncService;
/**
* 测试
* @author CA
*
*/
@Service
public class TestAsyncServiceImpl implements TestAsyncService {
@Override
public void serviceTest() {
// 这里执行实际的业务逻辑,在这里我们就是用一个简单的遍历来模拟
Arrays.stream(new int[]{1,2,3,4,5,6,7,8,9,10}).forEach( t -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("获取number为:" + t) ;
});
}
}
SERVICE2
public interface TestAsyncService2 {
public void serviceTest();
}
SERVICEIMPL2
import java.util.Arrays;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import com.carlo.test.spboottest.service.TestAsyncService2;
/**
* 测试使用注解@EnableAsync和@Async来实现 异步调用
* @author cs
*
*/
@Service
@EnableAsync
public class TestAsyncServiceImpl2 implements TestAsyncService2 {
/**
* 采用异步执行
*/
@Async
@Override
public void serviceTest() {
// 这里执行实际的业务逻辑,在这里我们就是用一个简单的遍历来模拟
Arrays.stream(new int[]{1,2,3,4,5,6,7,8,9,10}).forEach( t -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("@Async 获取number为:" + t) ;
});
}
}
执行结果
http://localhost:8080/test
获取主线程名称:http-nio-8080-exec-1
获取number为:1
获取number为:2
获取number为:3
获取number为:4
获取number为:5
获取number为:6
获取number为:7
获取number为:8
获取number为:9
获取number为:10
执行成功,返回结果
http://localhost:8080/test1
ThreadPoolExecutor 获取主线程名称:http-nio-8080-exec-3
ThreadPoolExecutor 执行完成,向用户响应成功信息
获取number为:1
获取number为:2
获取number为:3
获取number为:4
获取number为:5
获取number为:6
获取number为:7
获取number为:8
获取number为:9
获取number为:10
http://localhost:8080/test3
@Async 获取主线程名称:http-nio-8080-exec-2
@Async 执行完成,向用户响应成功信息
@Async 获取number为:1
@Async 获取number为:2
@Async 获取number为:3
@Async 获取number为:4
@Async 获取number为:5
@Async 获取number为:6
@Async 获取number为:7
@Async 获取number为:8
@Async 获取number为:9
@Async 获取number为:10