package com.hz.pm.api.sys.manage;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @Classname CpuManage
 * @Description
 * @Date 2023/8/7 9:25
 * @Author PoffyZhang
 */
@Component
@Slf4j
public class CpuManage {
    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    private static volatile boolean flag = true;

    public void high() {
        flag = true;
        executorService.execute(() -> {
            while (flag) {
                // 一个复杂的计算任务
                long x = 123456789123456789L;
                for (int j = 0; j < 10000; j++) {
                    if(!flag){
                        break;
                    }
                    x = (x * 123456789123456789L) % 123456789;
                }
            }
        });
    }

    public void close(){
        try{
            executorService.shutdownNow();
            flag = false;
            executorService = Executors.newFixedThreadPool(10);
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }
}
