I would consider using ScheduledExecutorService
and rewrite your main method in this way:
public class demoClass{
public void main(){
Runnable task1 = () -> {
switch (StateMachine()) { //statemachine determines logic between states
case s10_StandBy:
doSomething_1();
break;
case s20_NormalStartOrFlush:
doSomething_2();
break;
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(task1, 0, 250, TimeUnit.MILLISECONDS);
}
public void doSomething_1(){
// for example a one second delay is implemented here
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…