【问题标题】:How to cancel scheduled Timer Task and run it again?如何取消预定的定时器任务并再次运行?
【发布时间】:2021-07-15 12:25:12
【问题描述】:

首先这是一个 Minecraft Forge 模组,但我相信你不需要知道 forge 来解决我的问题。

我只是使用键绑定“K”来安排 3 个不同的计时器,然后单击“L”以取消它们再次运行。在我(再次)启动“K”以再次启动计时器之前没有问题,但我得到了

“java.lang.IllegalStateException:计时器已取消。”日志中的错误。

我用来管理计时器的代码:

public class KeyBind {
public static Boolean Status = false;
Timer timer = new Timer();



@SubscribeEvent
public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
    if (ForgeMain.keybinding.isKeyDown()) {
        if(Status == false) {
            EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
            entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + "You have successfully ran the NetherWart Macro!"));
            Status = true;
            timer.schedule(new Left(), 100, 38000);
            timer.schedule(new Forward(), 33100, 38000);
            timer.schedule(new Right(), 35100, 38000);
        }
    }if(ForgeMain.keybinding1.isKeyDown()){
        if(Status == true) {
            Status = false;
            System.out.println(Status.booleanValue());
            timer.cancel();
            timer.purge();
        }

        }
    }

这是我的一个计时器(它们的工作方式相同,不同之处在于移动方向和移动时间):

public class Right extends TimerTask {
@Override
public void run() {
    int left = Minecraft.getMinecraft().gameSettings.keyBindRight.getKeyCode();

    long oldTime = System.currentTimeMillis();

    while (System.currentTimeMillis() - oldTime < 33000) {
        KeyBinding.setKeyBindState(left, true);
    }
    KeyBinding.setKeyBindState(left, false);

}

}

编辑:

好吧,我知道我现在必须创建另一个计时器:

    @SubscribeEvent
public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
    if (ForgeMain.keybinding.isKeyDown()) {
        if(Status == false) {
            EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
            entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + "You have successfully ran the NetherWart Macro!"));
            Status = true;
            Timer timer = new Timer();
            timer.schedule(new Left(), 100, 38000);
            timer.schedule(new Forward(), 33100, 38000);
            timer.schedule(new Right(), 35100, 38000);
        }
    }if(ForgeMain.keybinding1.isKeyDown()){
        if(Status == true) {    
            Status = false;
            System.out.println(Status.booleanValue());
            timer.cancel();
            timer.purge();
        }

        }
    }

}

但现在我不知道如何取消计时器(在代码 timer.cancel();ofc 不起作用。)

【问题讨论】:

  • 根据文档:cancel() 取消 TimerTask 并将其从 Timer 的队列中删除。由于它已从计时器队列中删除,因此您必须创建一个新的。
  • 这是java.util.Timer?我问,因为 API 文档建议重复调用 cancelpurge 应该不是问题。如果是那个类,我会倾向于创建Timer 并安排它
  • 哦,我明白你是eeedev。但是,如果我通过单击“k”(使用 Timer timer = new Timer();)创建新计时器,我将无法通过按“L”来取消它。您对此有解决方案吗?
  • 不要移动Timer的声明。将它初始化为 null 它 曾经 并在您的 status == false 块中创建它

标签: java


【解决方案1】:

timer.cancel() 终止计时器,因此它不能再用于安排新任务。取消计时器后,您将需要实例化一个新计时器。

public class KeyBind {
    public static Boolean Status = false;
    Timer timer = new Timer();

    @SubscribeEvent
    public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
        if (ForgeMain.keybinding.isKeyDown()) {
            if(Status == false) {
                EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
                entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + "You have successfully ran the NetherWart Macro!"));
                Status = true;
                timer.schedule(new Left(), 100, 38000);
                timer.schedule(new Forward(), 33100, 38000);
                timer.schedule(new Right(), 35100, 38000);
            }
        }if(ForgeMain.keybinding1.isKeyDown()){
            if(Status == true) {
                Status = false;
                System.out.println(Status.booleanValue());
                timer.cancel();
                timer = new Timer();
            }

        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2017-07-05
    • 2013-09-16
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多