If you prefer a more readable and self-explanatory approach:
import java.util.concurrent.TimeUnit;
public class SleepExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
try {
TimeUnit.SECONDS.sleep(1); // More readable
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
- TimeUnit.SECONDS.sleep(1) is clearer than Thread.sleep(1000).