Why Isn’t My Thread Sleeping Correctly?

Profile Picture

Henrique Sa

OP
Admin
@riquelds.bsky.social
3 days ago

The program should print a message every second, but it prints everything instantly. What’s wrong?

for (int i = 0; i < 5; i++) {
    System.out.println("Tick " + i);
    Thread.sleep(1000); // Error: Unhandled exception
}
java oop multithreading