1. Thread.sleep()

You can insert pauses into the execution of a Java program. This isn't usually necessary, since users want their programs to run as quickly as possible. Not many people would be happy if you deliberately slowed down your code.

But as a programmer, there can be tons of situations where a pause in your code will be useful. For example, suppose you're writing a game and you want it to do something once every two seconds or several times a second.

Basically, pauses are useful, so let's see how to add a pause to your code. It's actually very simple:

Thread.sleep(duration);

Where duration is the length of the pause in milliseconds (1/1000 of a second).

This statement will pause your program for duration milliseconds. Examples:

Thread.sleep(2000);
Pauses the program for 2 seconds.
Thread.sleep(500);
Pauses the program for half a second.
Thread.sleep(60 * 60 * 1000);
Pauses the program for 1 hour.

Here's how it can be used in practice. Let's say we're writing a program that will launch a spaceship. This is how the code might look like:

for (int i = 10; i > 0; i--)
{
   System.out.println(i);
   Thread.sleep(1000);
}

System.out.println("Let's go!");
Each second, the program will display a number: 10, then 9, then 8, etc.




When the count reaches 0, the program will display "Let's go!"

2. Calculating a pause correctly

The length of the pause is easy to calculate. If you need the program to do something once a second, then the pause is 1000 ms. If 2 times per second, then pause for 500ms (1000/2).

If you need to do something 15 times per second, pause for 66 ms (1000/15). It all seems rather straightforward:

The duration of one iteration of the loop = 1000 / number of times per second

But there is a very important nuance here. Though many statements execute very quickly, they are not instantaneous.

Look at this. Let's say you have an action that takes 100ms to complete. You want to perform this action 5 times per second. How long should you pause? Definitely not 200ms.

For the action to be performed 5 times per second, we need the time required to execute the action plus the pause duration to be equal to 200 ms. If we do that, then it will run precisely 5 times per second. In our case, the action requires 100 ms, which means that there is still 100 ms left for the pause.

pause duration = duration of one iteration of the loop - time required to execute the action

Game developers are well aware of the fact that the time required to execute an action is much higher than zero. And so are the people who play games.

If a game runs at 20 FPS, that means that it can draw only 20 frames on the screen in one second. 1000/20 yields 50 ms. This is the time required to draw a frame while playing the game.



3. Nanoseconds

Computers today are much faster than when Java was created. That means that a pause of 1 millisecond may not be sufficiently granular.

Suppose we have some super brief action that we want to perform 2000 times per second. How do we pause for half a millisecond?

For this, there is one more variant of the Thread.sleep() method:

Thread.sleep(milliseconds, nanoseconds);

This method puts the program to sleep for the specified amount of milliseconds and nanoseconds.

Nanoseconds are 1 millionth of a millisecond. That means that a pause of one and a half milliseconds would look like this:

Thread.sleep(1, 500_000);

And if you want a pause of 1/10 of a millisecond, you need to write this:

Thread.sleep(0, 100_000);

You might not be using this method in your programs right now. But it's better to know about it and not use it than to need it and not know about it.



4. TimeUnit class

By the way, Java has another class that will make your life easier if you decide to slow down your application. We're talking about the TimeUnit class in the java.util.concurrent package.

Remember that since the class isn't in the java.lang package, then you either need to add the line import java.util.concurrent.TimeUnit;, or write java.util.concurrent.TimeUnit each time in your code.

This class does the same thing as Thread.sleep(), but it's more convenient:

TimeUnit.HOURS.sleep(15)

This code will put your program to sleep for 15 hours. Minutes, seconds, days are also available. As well as microseconds (1/1000,000) and nanoseconds (1/1000,000,000).

The TimeUnit class has the following properties:

  • nanoseconds: NANOSECONDS
  • microseconds: MICROSECONDS
  • milliseconds: MILLISECONDS
  • seconds SECONDS
  • minutes: MINUTES
  • hours: HOURS
  • days: DAYS

Work with these properties is super convenient, since there is no need to think about converting, say, hours into milliseconds. Such code is much more pleasant to write and read.

More details can be read here.