1

我想使用该Thread.sleep(1000)命令在我的程序中添加延迟(因此在继续之前让它停止 1 秒),但这意味着我还需要添加throws InterruptedException. 但是,我不知道把它放在哪里。

我的代码现在基本上是这样的:

public static void main(String[] args) {
    Clock myClock = new Clock;   // new clock object.

    while (true) {
        myClock.tick();
    }
}

我的另一堂课:

public class Clock {
    // .. some constructors and stuff

    public void tick() {
        secondHand.draw();   // redraws the second hand to update every second
    }

    // .. some more methods and stuff
}

我只想tick()每 1 秒调用一次该方法,但我不知道在哪里可以放置Thread.sleepandthrows InterruptedException语句。任何帮助,将不胜感激。此外,我可以让我的时钟滴答作响和/或更新的其他方式的任何输入也将有所帮助!

4

5 回答 5

5

我只想每 1 秒调用一次 tick() 方法

调用后tick(),在当前线程上休眠,如下所示:

public static void main(String[] args) {
    Clock myClock = new Clock;   // new clock object.

    while (true) {
        myClock.tick();
        // wait for a second.
        try {
            Thread.sleep(1000);
        }
        catch (InterruptedException ie) {
            // Handle the exception
        }
    }
}
于 2012-10-30T03:30:45.390 回答
1

根据程序的流程,您可能希望将Thread.sleep(1000);语句放入控制逻辑中。我的意思是,tick()按照惯例,任何逻辑调用你的方法都是引入逻辑控制的最佳位置——在这种情况下是你的Thread.sleep(1000);语句。

唉,你的调用逻辑应该是这样的:

try {
    while (true) {
        Thread.sleep(1000);
        myClock.tick();
    }
} catch(InterruptedException e) {
    System.err.println("Something went wrong");
}

这种方法确保如果您有其他控制逻辑访问您的tick()方法,那么它不会被绑定到 1 秒延迟。不同的客户通常有不同的需求...

我更喜欢将整个 while 循环包装在 try catch 块中以实现可伸缩性安全可读性...我的意思是,如果您的tick()方法中的逻辑变得更加复杂并且您最终抛出更多异常,那么您可以简单地将 catch 子句添加到一个 try catch 块像这样:

try {
    while (true) {
        Thread.sleep(1000);
        myClock.tick();
    }
} catch(InterruptedException e) {
    System.err.println("Something went wrong");
} catch(SomeNewExcetion e) {
    System.err.println("Something went wrong");
} catch(SomeOtherNewExceptoion e) {
    System.err.println("Something went wrong");
} catch(SomeFinalException e) {
    System.err.println("Something went wrong");
}
于 2012-10-30T03:43:01.027 回答
1

您可以使用javax.swing.Timerjava.util.Timer来获得相同的结果。

javax.swing.Timer的优点是当它被触发时,该ActionPerformed方法在事件调度线程中执行,从而更容易更新 UI。

java.util.Timer

timer = new Timer();
timer.schedule(new TickTock (), 1000);

public class TickTock extends TimerTask {
    public void run() {
        System.out.println("Tick tock!");
    }
}

javax.swing.Timer

Timer timer = new Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Tick tock!");
        repaint();
    }
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();

您可以阅读如何使用摇摆计时器了解更多信息javax.swing.Timer

于 2012-10-30T03:44:33.620 回答
1

虽然我通常不会建议它。

在这种情况下,您实际上不需要为此被中断编写代码。通过使用运行时异常包装来隐藏它。

public class Clock {
    // .. some constructors and stuff

    public void tick() {
       secondHand.draw();   // redraws the second hand to update every second
    }
    public void wait() {
       try {
           Thread.sleep(1000)
       } catch(InterruptedException e) {
           throw new RuntimeException("Don't know how to handle this", e);
       }
    }
   // .. some more methods and stuff
}
于 2012-10-30T03:59:19.533 回答
0

它在java中非常简单

TimeUnit.SECONDS.sleep(2);
于 2015-09-16T12:25:58.330 回答