package J20250731.demo03_threadPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.*;

/**
 * @author WanJl
 * @version 1.0
 * @title ThreadPoolDemo04
 * @description
 * @create 2025/7/31
 */
public class ThreadPoolDemo04 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < 2000000; i++) {
            int age = (int) (Math.random() * 100);
            int n = i + 1;
            Student s = new Student(n, "学生" + n, age, age % 2 == 0 ? "男" : "女");
            list.add(s);
        }
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                2,
                5,
                20,
                TimeUnit.MICROSECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        long start = System.currentTimeMillis();
        pool.submit(() -> {
            //获取每个6年龄的学生有多少位(需要用到HashMap)
            HashMap<Integer, Integer> map1 = new HashMap<>();
            for (Student s : list) {
                Integer age1 = s.getAge();
                map1.put(age1, map1.getOrDefault(age1, 0) + 1);
            }
            System.out.println("每个年龄的学生数量是：" + map1);
        });

        pool.submit(() -> {
            //获取男生多少位，女生多少位
            HashMap<String, Integer> map2 = new HashMap<>();
            for (Student s : list) {
                String sex = s.getSex();
                map2.put(sex, map2.getOrDefault(sex, 0) + 1);
            }
            System.out.println("男女生数量分别是是：" + map2);
        });
        pool.shutdown();
        long end = System.currentTimeMillis();
        System.out.println("程序执行总耗时：" + (end - start) + "毫秒");


    }
}
