使用双异步后 如何保证数据一致性?(3)
导读:使用双异步后,2、将异步方法的返回值改为 FutureInteger ,将返回值放到 new AsyncResult(); 中; @Async("async-executor")public void readXls(String filePath, String filename) { try { // 此代码为简
使用双异步后
2、将异步方法的返回值改为Future<Integer>,将返回值放到new AsyncResult<>();中;
@Async("async-executor")
public void readXls(String filePath, String filename) {
try {
// 此代码为简化关键性代码
List<Future<Integer>> futureList = new ArrayList<>();
for (int time = 0; time < times; time++) {
Future<Integer> sumFuture = readExcelDataAsyncFutureService.readXlsCacheAsync();
futureList.add(sumFuture);
}
}catch (Exception e){
logger.error("readXlsCacheAsync---插入数据异常:",e);
}
}
@Async("async-executor")
public Future<Integer> readXlsCacheAsync() {
try {
// 此代码为简化关键性代码
return new AsyncResult<>(sum);
}catch (Exception e){
return new AsyncResult<>(0);
}
}
3、通过Future<Integer>.get()获取返回值:
public static boolean getFutureResult(List<Future<Integer>> futureList, int excelRow){
int[] futureSumArr = new int[futureList.size()];
for (int i = 0;i<futureList.size();i++) {
try {
Future<Integer> future = futureList.get(i);
while (true) {
if (future.isDone() && !future.isCancelled()) {
Integer futureSum = future.get();
logger.info("获取Future返回值成功"+"----Future:" + future
+ ",Result:" + futureSum);
futureSumArr[i] += futureSum;
break;
} else {
logger.info("Future正在执行---获取Future返回值中---等待3秒");
Thread.sleep(3000);
}
}
} catch (Exception e) {
logger.error("获取Future返回值异常: ", e);
}
}
boolean insertFlag = getInsertSum(futureSumArr, excelRow);
logger.info("获取所有异步线程Future的返回值成功,Excel插入结果="+insertFlag);
return insertFlag;
}
4、这里也可以通过新线程+Future获取Future返回值
@Async("async-executor")
public void readXls(String filePath, String filename) {
try {
// 此代码为简化关键性代码
List<Future<Integer>> futureList = new ArrayList<>();
for (int time = 0; time < times; time++) {
Future<Integer> sumFuture = readExcelDataAsyncFutureService.readXlsCacheAsync();
futureList.add(sumFuture);
}
}catch (Exception e){
logger.error("readXlsCacheAsync---插入数据异常:",e);
}
}@Async("async-executor")
public Future<Integer> readXlsCacheAsync() {
try {
// 此代码为简化关键性代码
return new AsyncResult<>(sum);
}catch (Exception e){
return new AsyncResult<>(0);
}
}Future<Integer>.get()获取返回值:public static boolean getFutureResult(List<Future<Integer>> futureList, int excelRow){
int[] futureSumArr = new int[futureList.size()];
for (int i = 0;i<futureList.size();i++) {
try {
Future<Integer> future = futureList.get(i);
while (true) {
if (future.isDone() && !future.isCancelled()) {
Integer futureSum = future.get();
logger.info("获取Future返回值成功"+"----Future:" + future
+ ",Result:" + futureSum);
futureSumArr[i] += futureSum;
break;
} else {
logger.info("Future正在执行---获取Future返回值中---等待3秒");
Thread.sleep(3000);
}
}
} catch (Exception e) {
logger.error("获取Future返回值异常: ", e);
}
}
boolean insertFlag = getInsertSum(futureSumArr, excelRow);
logger.info("获取所有异步线程Future的返回值成功,Excel插入结果="+insertFlag);
return insertFlag;
}不过感觉多此一举了,就当练习Future异步取返回值了~
public static Future<Boolean> getFutureResultThreadFuture(List<Future<Integer>> futureList, int excelRow) {
ExecutorService service = Executors.newSingleThreadExecutor();
final boolean[] insertFlag = {false};
service.execute(new Runnable() {
public void run() {
try {
insertFlag[0] = getFutureResult(futureList, excelRow);
} catch (Exception e) {
logger.error("新线程+Future获取Future返回值异常: ", e);
insertFlag[0] = false;
}
}
});
service.shutdown();
return new AsyncResult<>(insertFlag[0]);
}
获取异步线程结果后,我们可以通过添加事务的方式,实现Excel入库操作的数据一致性。
以上分享的使用双异步后 和 如何保证数据一致性的详细讲解,IT袋网小编希望本文能给你带来生活上的帮助!
相关阅读
-
企业网站建设要多少钱 建企业网站需要的费用
小编为大家说一说企业网站建设要多少钱和建企业网站需要的费用的电脑小知识,具体详情如下: 搭建一个企业网站需要多少钱? 其实现在搭建企业网站比较透明了,你如果用模板搭建的话,
-
dns服务器不可用是什么原因 网络dns异常修复技巧
小编为网友们解答dns服务器不可用是什么原因和网络dns异常修复技巧的电脑小知识,接下来IT袋小编为大家介绍。 我们在使用电脑的时候经常会遇到各种各样的网络问题,例如最近就有Win11电脑
-
gdb怎么调试的?打断点用什么指令?
一个电脑小知识,为大家介绍gdb怎么调试的的内容,下面为详细的介绍。 编译时添加调试信息:在编译程序时,需要使用-g选项,以便将调试信息嵌入可执行文件中。例如: g++ -g -o my_program
-
创建一个自己的网站怎么弄 网页设计与网站建设教程
小编为你解答创建一个自己的网站怎么弄和网页设计与网站建设教程的IT小经验,具体详情如下: 前言: 现在很多人都拥有自己的网站,IT袋网小编在业余时间也搭建了一个自己的个人博客网


