I make Java applications. It became necessary, under certain conditions, to pause program execution for a long time (an hour or even more). The code is executed in a separate thread.
How correctly will it be used for such a long pause

Thread.sleep(*очень большое число*) 

Maybe there is another, more correct way?

  • In theory, an InterruptedException may occur. Maybe it's better to use something like Timer , TimerTask ? - Vladyslav Matviienko
  • Not only that, the application can close in this hour. - KoVadim
  • @metalurgus timer starts a separate thread after some time, but does not pause the program. To pause the main thread will come up with a way to synchronize it with the timer. It will be harder to sleep - Sergey
  • one
    @Sergey, you still tell me that using goto is not a mortal sin. In addition, in the secondary thread - use sleep as much as fit. But if you want to use it in the main thing - this is wrong, disgusting and terrible by definition. - Vladyslav Matviienko
  • one
    Then it is more correct to alter so as to use timers. The second thread completed part of the work, ended. Continuation is started by the timer. How do metalurgus talk about this in the comments and Mikhailov Valentine in his answer - Sergey

3 answers 3

It would be more correct to use a timer or ScheduledExecutor (ohm) for example like this:

 Timer t = new Timer(); t.scheduleAtFixedRate(new TimerTask() { public void run() { System.out.println("do task"); } }, 0, 100); 

 ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleWithFixedDelay( () -> { System.out.println("do task"); }, 0, 1, TimeUnit.HOURS); 
  • What does the program do after doing t.scheduleAtFixedRate(...) or service.scheduleWithFixedDelay(...) ? It continues to be executed, and according to the condition of the task it should stop. Make some kind of synchronization with the task? But why is it worse than sleep , except that two threads will be involved instead of one in sleep? - Sergey
  • @Sergey, can continue and stop, describe in more detail what is meant by "stop"? - Mikhailov Valentine
  • Read the question carefully. It is necessary to паузу выполнения программы . Will your decision pause? It is doubtful - Sergey
  • @Sergey, to be honest, without your comment, I would not understand this question. Most embarrassing "The code is executed in a separate thread.", But oh well, then perhaps sleep will be easier. - Mikhailov Valentine
  • Also embarrassing. In short, FIG knows what they need - Sergey
 try { Thread.sleep(1000 * 60 * 60); } catch (InterruptedException ex) {} 
  • one
    InterruptedException should never be swallowed. Imagine that the author will now take advantage of your advice and place his code (which should be executed in an hour) immediately after yours; in this case, the interrupt will trigger its execution before the required time. - etki
  TimeUnit.SECONDS.sleep(1); TimeUnit.MINUTES.sleep(1); TimeUnit.HOURS.sleep(1); TimeUnit.DAYS.sleep(1); 

is not satisfied?