多线程-Future Task
在开发中如果碰到处理稍大数据量的场景可以考虑使用多线程来提高效率. 用法如示例:
org.apache.commons
commons-math3
3.6.1
2.示例
import com.alibaba.fastjson.JSON;
import dintalk.cn.common.demo.Student;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import org.apache.commons.math3.stat.StatUtils;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
/**
* @ClassName: FutureTaskDemo
* @Description:
* @Author: song hui
* @Date: 2022/1/14
*/
public class FutureTaskDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 需求:
* 统计 各个年级中,学生的分数处于 所在年级 所有学生分数计算的 μ +- 1.5δ 之外. 的学生
*/
Map result = new HashMap<>();
// 1. 获取 学生, 按照年级分组
List studentList = getStudents();
Map> studentMap = studentList.stream().collect(Collectors.groupingBy(Student::getGrade));
// 2. 创建多线程 task, 处理数据
Map> allTaskMap = new HashMap<>();
ExecutorService executor = Executors.newFixedThreadPool(studentMap.size());
// 3. 按照年级 , 提交 task
for (Map.Entry> gradeEntry : studentMap.entrySet()) {
Future