Large delay (pause) in Java

Question:

I make Java applications. There was a need, when certain conditions were met, to pause the execution of the program for a long time (an hour or even more). The code is executed in a separate thread.
How correct would it be to use for such a long pause

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

Maybe there is another, better way?

Answer:

It would be more correct to use a timer or ScheduledExecutor (th) 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);
Scroll to Top