java—如何在每次使用不同参数调用方法时创建一个新示例?

v1uwarro  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(307)

在我的应用程序中,我必须使用外部库,为了减少多余的代码,我想创建一个方法“startdelay”,每次调用它时,它都会创建一个新的计时器并保持运行,即使使用不同的参数再次调用该方法。
但是当我调用这个方法时,它似乎会重置,并且不会创建新的计时器。
例如:
我有5秒和10秒的延迟。所以在5s一开始的几秒钟后,我想开始其他10s的延迟(创建一个新的)5秒延迟将不会完成其原来的计数,只是用新的计时器重新开始。新电话接通时,计时器不会结束。
我怎样才能解决这个问题?提前谢谢!

public class BIhcsTestModule extends BComponent implements Runnable {

    Clock.Ticket[] delayTimer = new Clock.Ticket[10];
    int count = 0;

    public void run() {System.out.println("Source BProgram did not 
    override run(). Exiting thread.");
}
    //Main
     public void doExecute() throws Exception {
     try {
        if (getBBool_1()) {
            startDelay(getTTime_1(), action1, 21);
        }
        if (getBBool_2()) {
            startDelay(getTTime_2(), action2, 22);
        }

    } catch (Exception e) {
        System.out.println(e);
    }
}           

    public void startDelay(BRelTime delayTime, String 
    actionToTrigger, Integer newStatus) {
        //reset count
        if (count >= delayTimer.length) {
            count = 0;
        }
        if (count < delayTimer.length) {

            if (delayTimer[count] != null) {
                delayTimer[count].cancel();
                delayTimer[count] = null;
            }

            //if time is set
            if (delayTime.getSeconds() > 0) {
                this.delayTimer[count] = Clock.schedule(this.getComponent(), delayTime, this.getComponent().getAction(actionToTrigger), (BValue) new BStatusNumeric(newStatus));
            }
        } else {
            setDebugLine("t not >0");
        }
        count++;
    }
}
x8goxv8g

x8goxv8g1#

startdelay方法在代码中似乎很好。检查是否对所有startdelay方法调用使用相同的bihcstestmodule示例。

相关问题