1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.dy.common.mw.core;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
/**
 * @Author liurunyu
 * @Date 2023/12/19 16:41
 * @LastEditTime 2023/12/19 16:41
 * @Description
 */
public class CoreConstantThread extends Thread {
 
    private final static Logger log = LogManager.getLogger(CoreConstantThread.class.getName()) ;
 
    private long sleepBigBusy ;
    private long sleepSmallBusy ;
    private CoreTask task ;
 
    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);
                }
            }
        }
    }
}