0

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.

ruohola
  • 21,987
  • 6
  • 62
  • 97
  • basically, you want a Timer to hold your code for 2 hours, and after two hours print the second line? – Stultuske Jan 30 '20 at 11:19
  • 3
    Does this answer your question? [How do I make a delay in Java?](https://stackoverflow.com/questions/24104313/how-do-i-make-a-delay-in-java) – ruohola Jan 30 '20 at 11:20
  • That helped too, thanks #ruohola –  Jan 30 '20 at 11:30
  • Don't let a Thread sleep for two hours. Use timers or similar constructs instead to execute specific actions in the future. For example have a look [this question](https://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java) – Burdui Jan 30 '20 at 11:33

4 Answers4

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.

Amongalen
  • 3,101
  • 14
  • 20
  • Actually, just adding `executorService.shutdown()` at the end of the `main` method above should work. I'm just dumb :). – BeUndead Jan 30 '20 at 11:57
0

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");
Smile
  • 3,832
  • 3
  • 25
  • 39
  • 3
    `120_000` = 2 minutes. `TimeUnit.HOURS.sleep(2L);` would be more readable, less error prone, and actually sleep the correct duration. That said, sleeping for 2 hours is a horrible idea for a `Thread`. – BeUndead Jan 30 '20 at 11:22
  • I am making a time table, and a setting to say how much to wait then give an alarm, so if the user wanted for examples 2 hours, it should wait 2 hours and then give an alarm. –  Jan 30 '20 at 11:29
0

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.

Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25
0

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");
Suryakant Bharti
  • 673
  • 1
  • 6
  • 24