liurunyu
2024-05-17 22139031145838326262323b61d61ecc46617397
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/core/CoreConstantThread.java
@@ -1,64 +1,45 @@
package com.dy.common.mw.core;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.*;
/**
 * @Author liurunyu
 * @Date 2023/12/19 16:41
 * @LastEditTime 2023/12/19 16:41
 * @Description
 */
public class CoreConstantThread extends Thread {
public class CoreConstantThread extends Thread{
   private final static Logger log = LogManager.getLogger(CoreConstantThread.class.getName()) ;
   private final static CoreConstantThread instance = new CoreConstantThread() ;
    private final static Logger log = LogManager.getLogger(CoreConstantThread.class.getName()) ;
   @SuppressWarnings("unused")
   private static Long sleepBigBusy = 100L ;//大忙时(除了恒久任务,还是其他任务),核心线程暂停间隔
   private static Long sleepSmallBusy = 500L ;//小忙时(只有恒久任务,无其他任务),核心线程暂停间隔
   private CoreConstantThread(){
   }
   public static CoreConstantThread getInstance(){
      return instance ;
   }
   /**
    * 设置暂停时长
    * @param sleepBigBusy 大忙时(除了恒久任务,还是其他任务),核心线程暂停间隔
    * @param sleepSmallBusy 小忙时(只有恒久任务,无其他任务),核心线程暂停间隔
    */
   public void setSleep(Long sleepBigBusy, Long sleepSmallBusy){
      CoreConstantThread.sleepBigBusy = sleepBigBusy ;
      CoreConstantThread.sleepSmallBusy = sleepSmallBusy ;
   }
    private long sleepBigBusy ;
    private long sleepSmallBusy ;
    private CoreTask task ;
   /**
    * 核心单线程,执行所有的单线程任务
    */
   @Override
   @SuppressWarnings("InfiniteLoopStatement")
   public void run(){
      int count ;
      Integer temp ;
      while (true) {
         count = 0;
         try {
            //恒久任务
            List<CoreTask> constantTasks = CoreUnit.getAllConstantTasks();
            if (constantTasks != null && constantTasks.size() > 0) {
               for (CoreTask task : constantTasks) {
                  temp = task.execute();
                  if (temp != null) {
                     count += temp;
                  }
               }
            }
            if (count == 0) {
               //小暂停一下
               Thread.sleep(sleepSmallBusy);
            }
         } catch (Exception e) {
            log.error("核心线程发生异常" + (e.getMessage() == null ? "" : (":" + e.getMessage())), e);
         }
      }
   }
    public CoreConstantThread(long sleepBigBusy, long sleepSmallBusy, CoreTask task){
        this.sleepBigBusy = sleepBigBusy ;
        this.sleepSmallBusy = sleepSmallBusy ;
        this.task = task ;
    }
    @Override
    public void run() {
        if(task != null){
            int count ;
            while (true) {
                try {
                    count = task.execute();
                    if (count == 0) {
                        //小暂停一下
                        Thread.sleep(sleepBigBusy);
                    }else{
                        Thread.sleep(sleepSmallBusy);
                    }
                } catch (Exception e) {
                    log.error("恒久任务" + task.getClass().getName() + "执行时发生异常" + (e.getMessage() == null ? "" : (":" + e.getMessage())), e);
                }
            }
        }
    }
}