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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Pausing execution of a thread3m 52s
-
(Locked)
Interrupting a thread5m 42s
-
(Locked)
Implementing thread interruptions6m 13s
-
(Locked)
Making a thread to wait until another thread terminates5m 39s
-
(Locked)
Other useful methods of the Thread class2m 42s
-
(Locked)
Challenge: Webinar polling system1m 53s
-
(Locked)
Solution: Webinar polling system5m 9s
-
(Locked)
-