From the course: Java Threads

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Pausing execution of a thread

Pausing execution of a thread - Java Tutorial

From the course: Java Threads

Pausing execution of a thread

- [Instructor] In your program, you might want the currently executing thread to temporarily pause its execution for a specified amount of time. For instance, let's say your main thread is performing some task. Java threads allow you to pause for a while, wait for some time and then continue to perform the rest of the task. In the thread class, there's a static method named sleep to do that. You can pass the amount of time you want the current thread to pause it's task for in milliseconds. There's an overloaded sleep method that takes in a millisecond value plus a nanosecond value, if you want to be more precise in specifying the time the current thread needs to pause itself. When the sleep method is called on a thread that is running, it goes into a waiting state for the specified amount of time. Once the wait time is over, the thread goes back to being runable and waits for the CPU to run its task once again. In the…

Contents