Pause Thread Execution
Thread Scheduling:
A processor’s time is usually time-sliced to allow multiple threads to run, with each thread using one or more time slices. A thread scheduler might suspend a running thread and move it to the ready state, executing another thread from the ready queue. Thread scheduling is specific to JVM implementation and beyond the control of an application programmer. The scheduler moves a thread from the READY state to RUNNING and vice versa, to support concurrent processing of threads.
Thread Priorities:
Remember that all the threads carry normal priority when a priority is not specified.
Priorities can be specified from 1 to 10. 10 being the highest, 1 being the lowest priority and 5 being the normal priority.
Remember that the thread with highest priority will be given preference in execution. But there is no guarantee that it will be in running state the moment it starts.
Always the currently executing thread might have the higher priority when compared to the threads in the pool who are waiting for their chance.
It is the thread scheduler which decides what thread should be executed.
t.setPriority() can be used to set the priorities for the threads.
Remember that the priorities should be set before the threads start method is invoked.
You can use the constants, MIN_PRIORITY,MAX_PRIORITY and NORM_PRIORITY for setting priorities.
1) yield() :
Theoretically, to ‘yield’ means to let go, to give up, to surrender. A yielding thread tells the virtual machine that it’s willing to let other threads be scheduled in its place. This indicates that it’s not doing something too critical. Note that it’s only a hint, though, and not guaranteed to have any effect at all.
yield() is defined as following in Thread.java.
Let’s list down important points from above definition:
Yield is a Static method and Native too.
Yield tells the currently executing thread to give a chance to the threads that have equal priority in the Thread Pool.
There is no guarantee that Yield will make the currently executing thread to runnable state immediately.
It can only make a thread from Ready State to Running State, not in wait or blocked state.
Example :
In below example program, I have created two threads named producer and consumer for no specific reason. Producer is set to minimum priority and consumer is set to maximum priority. I will run below code with/without commenting the line Thread.yield(). Without yield(), though the output changes sometimes, but usually first all consumer lines are printed and then all producer lines.
With using yield() method, both prints one line at a time and pass the chance to another thread, almost all the time.
Output of above program “without” yield() method
Output of above program “with” yield() method added
2) join()
java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join(); it causes the current thread to pause its execution until thread it join completes its execution. If there are multiple threads calling the join() methods that means overloading on join allows the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. There are three overloaded join functions.
join(): It will put the current thread on wait until the thread on which it is called is dead. If thread is interrupted then it will throw InterruptedException. Syntax:
join(long millis) :It will put the current thread on wait until the thread on which it is called is dead or wait for specified time (milliseconds). Syntax:
join(long millis, int nanos): It will put the current thread on wait until the thread on which it is called is dead or wait for specified time (milliseconds + nanos). Syntax:
Example :
As we can see , main() thread was sent to waiting state when producerThread.join() was called.Hence after the completion of producerThread main() thread resumed its execution.
3) sleep()
This method causes the currently executing thread to sleep for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. Syntax:
Output:
Java Thread Sleep important points
It always pause the current thread execution.
The actual time thread sleeps before waking up and start execution depends on system timers and schedulers. For a quiet system, the actual time for sleep is near to the specified sleep time but for a busy system it will be little bit more.
Thread sleep doesn’t lose any monitors or locks current thread has acquired.
Any other thread can interrupt the current thread in sleep, in that case InterruptedException is thrown.
How Thread Sleep Works
Thread.sleep() interacts with the thread scheduler to put the current thread in wait state for specified period of time. Once the wait time is over, thread state is changed to runnable state and wait for the CPU for further execution. So the actual time that current thread sleep depends on the thread scheduler that is part of operating system.
Last updated