英文:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论