package J20250801.homework;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author WanJl
 * @version 1.0
 * @title Demo08
 * @description
 * @create 2025/8/1
 */
public class Demo08 {
    private static int successCount=0;  //这个值是有问题的
    private static int errorCount=0;
    public static void main(String[] args) {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                10,
                20,
                30,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
                );
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            int x=i+1;
            poolExecutor.submit(()->{
                    try {
                        Thread.sleep(500);    //每条模拟耗时5ms
                        successCount+=100; //成功1次
                    } catch (InterruptedException e) {
                        errorCount+=100;
                    }
                System.out.println("批次["+x+"]导入完成，成功["+successCount+"]条");
                System.out.println("批次["+x+"]导入被中断，失败["+errorCount+"]条");
            });
        }


        poolExecutor.shutdown();
        try {
            if (!poolExecutor.awaitTermination(10,TimeUnit.MICROSECONDS)){
                poolExecutor.shutdownNow();
            }
        } catch (InterruptedException e) {
            poolExecutor.shutdownNow();
        }


        long end = System.currentTimeMillis();
        long time=end-start;
        System.out.println("总耗时："+time+"毫秒");
        System.out.println("成功["+successCount+"]条");
        System.out.println("失败["+errorCount+"]条");
    }
}
