如何在代码行之间设置等待时间。

huangapple go评论82阅读模式
英文:

How to make a wait period between the lines of code

问题

我想在代码中创建一个等待时间,就像这样:

javax.swing.JOptionPane.showMessgeDialog(null, "剩余2小时");
// 等待2小时
javax.swing.JOptionPane.showMessageDialog(null, "2小时前");

但我不知道如何实现这个等待时间,我会尝试所有的答案并选择最好的,但请您回答我。

英文:

I want to create a wait period between code like this:

javax.swing.JOptionPane.showMessgeDialog(null, "2 hours left");
// Wait for 2 hours
javax.swing.JOptionPane.showMessageDialog(null, "2 hours ago");  

But i don't know how to make this wait period, i will try all answers and chose the best one but please answer me.

答案1

得分: 2

所有其他答案都建议使用 Thread.sleep()。我认为这可能有效,但它会阻塞当前线程。通常情况下,您不希望出现这种情况。

您应该改为使用更稳健的解决方案,即使用ScheduledExecutorService及其schedule方法。像这样的代码应该适合您的情况:

public static void main(String[] args) {
    final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    long delay = 2L;
    executorService.schedule(App::myTask, delay, TimeUnit.HOURS);
}

private static void myTask() {
    //在延迟后执行您想要的操作
    System.out.println("Running");
}

myTask 方法内部的代码将在延迟2小时后在另一个线程中执行。

英文:

All the other answers suggest using Thread.sleep(). I suppose it could work but it blocks the current thread. Usually you don't want that.

You should use instead more robust solution which is a ScheduledExecutorService and its schedule method. Something like this should work for you:

public static void main(String[] args) {
    final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    long delay = 2L;
    executorService.schedule(App::myTask, delay, TimeUnit.HOURS);
}

private static void myTask() {
    //whatever you want to execute after delay
    System.out.println("Running");
}

The code inside of the myTask method will execute in some other thread after 2h delay.

答案2

得分: 0

以下是您要的翻译内容:

javax.swing.JOptionPane.showMessageDialog(null, "还剩下2小时");
// 等待2小时
try {
    Thread.sleep(7200000);// 时间间隔以毫秒为单位
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
javax.swing.JOptionPane.showMessageDialog(null, "2小时前");

或者,如@user2478398所建议:

javax.swing.JOptionPane.showMessageDialog(null, "还剩下2小时");
// 等待2小时
TimeUnit.HOURS.sleep(2L);
javax.swing.JOptionPane.showMessageDialog(null, "2小时前");
英文:

You are looking for

javax.swing.JOptionPane.showMessgeDialog(null, "2 hours left");
// Wait for 2 hours
try {
    Thread.sleep(7200000);// Time duration in miliseconds
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
javax.swing.JOptionPane.showMessageDialog(null, "2 hours ago");

Alternatively, as @user2478398 suggested

javax.swing.JOptionPane.showMessgeDialog(null, "2 hours left");
// Wait for 2 hours
TimeUnit.HOURS.sleep(2L);
javax.swing.JOptionPane.showMessageDialog(null, "2 hours ago");

答案3

得分: 0

这是你使用 Thread.sleep 方法的地方,这里是文档

javax.swing.JOptionPane.showMessageDialog(null, "剩余2小时");
// 等待2小时
Thread.sleep(7200000);
javax.swing.JOptionPane.showMessageDialog(null, "2小时前");

你可以在你的方法内部抛出 InterruptedException,或者你可以使用 try-catch 来处理它。

英文:

This is where you use Thread.sleep method, here is the documentation:

javax.swing.JOptionPane.showMessgeDialog(null, "2 hours left");
// Wait for 2 hours
Thread.sleep(7200000);
javax.swing.JOptionPane.showMessageDialog(null, "2 hours ago");  

you can either throw InterruptedException inside your method, or you can handle it using try-catch.

答案4

得分: 0

> 像这样添加延迟

    javax.swing.JOptionPane.showMessageDialog(null, "还剩2小时");
    TimeUnit.HOURS.sleep(2); // 等待2小时
    javax.swing.JOptionPane.showMessageDialog(null, "2小时前");
英文:

> Add delay like this:

javax.swing.JOptionPane.showMessgeDialog(null, "2 hours left");
TimeUnit.HOURS.sleep(2); // Wait for 2 hours
javax.swing.JOptionPane.showMessageDialog(null, "2 hours ago");

huangapple
  • 本文由 发表于 2020年1月30日 19:18:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定