需求:主要数据已经导入,需要使用已导入的数据计算一些值,计算可以延后使用异步的方式进行
解决方式:
方式一
public static void main(String[] args) {
Runnable r1 = () -> {
System.out.println("000000");//这里写计算方式
};
ThreadUtil.execute(r1);//异步执行
}
方式二:Score中调用计算方法,Score需要实现Runnable并重写run()方法,然后开启线程
package xxx;
import cn.stylefeng.guns.modular.physique.entity.AdultCondition;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
public class Score implements Runnable { //实现Runnable
@Autowired
private static DataService dataService = SpringContextHolder.getBean(DataService.class);
//存放构造器中的参数数据
private List<Data> dataList= null;
private String type = null;
public Score (List<Data> dataList, String type) {
//构造器中赋值参数数据
this.dataList= dataList;
this.type = type;
}
@Override
public void run() { //重写run()方法
//存放分数计算完成的datalist
List<Data> datas= new ArrayList<>();
//遍历构造器中传入的参数
for (Data data: dataList) {
//计算得分
Data getData= dataService.getScoreData(data, type);
datas.add(getData);
}
dataService.updateBatchById(datas);
}
}
开启线程
ThreadUtil.execute(new Score(list1,type));//异步线程计算得分
版权声明:本文为ZT1090258642原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。