| package com.dy.common.mw.core; | 
|   | 
| import java.util.List; | 
|   | 
| import org.apache.logging.log4j.*; | 
|   | 
| public class CoreConstantThread extends Thread{ | 
|      | 
|     private final static Logger log = LogManager.getLogger(CoreConstantThread.class.getName()) ; | 
|      | 
|     private final static CoreConstantThread instance = new CoreConstantThread() ; | 
|   | 
|     @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 ; | 
|     } | 
|   | 
|     /** | 
|      * 核心单线程,执行所有的单线程任务 | 
|      */ | 
|     @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.excute(); | 
|                         if (temp != null) { | 
|                             count += temp; | 
|                         } | 
|                     } | 
|                 } | 
|                 if (count == 0) { | 
|                     //小暂停一下 | 
|                     Thread.sleep(sleepSmallBusy); | 
|                 } | 
|             } catch (Exception e) { | 
|                 log.error("核心线程发生异常" + (e.getMessage() == null ? "" : (":" + e.getMessage())), e); | 
|             } | 
|         } | 
|     } | 
| } |