Hi! in this post i’ll be discussing difference between wait() and wait(long timeout) -What will be Thread states in detail when these methods are called.
difference between wait() and wait(long timeout) >
wait()
|
wait(long timeout)
|
When wait() method is called on object, it causes causes the current thread to wait until another thread invokes the notify() or notifyAll() method for this object.
|
wait(long timeout) - Causes the current thread to wait until either another thread invokes the notify() or notifyAll() methods for this object, or a specified timeout time has elapsed.
|
When wait() is called on object - Thread enters from running to waiting state.
It waits for some other thread to call notify so that it could enter runnable state.
|
When wait(1000) is called on object - Thread enters from running to waiting state. Than even if notify() or notifyAll() is not called after timeout time has elapsed thread will go from waiting to runnable state.
|
First, lets discuss wait() >
When wait() method is called on object, it causes causes the current thread to wait until another thread invokes the notify() or notifyAll() method for this object.
It’s important to know that wait() internally calls wait(0).
So, using using wait(0) in real time is not taken into consideration and the thread simply waits until notified by notify() or notifyAll().
So, in below program -
When wait() is called on object - Thread enters from running to waiting state.
It waits for some other thread to call notify so that it could enter runnable state. Deadlock is formed, These type of deadlocks are called Frozen processes.
class MyRunnable implements Runnable{
public void run(){
synchronized (this) {
System.out.println("MyRunnable,in run() method");
try {
this.wait(); //wait() internally calls wait(0)
System.out.println("Thread in waiting state, waiting for some "+
"other threads on same object to call notify() or notifyAll()");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class WaitNoParaMethod {
public static void main(String[] args) {
MyRunnable myRunnable=new MyRunnable();
Thread thread1=new Thread(myRunnable,"Thread-1");
thread1.start();
}
}
/*OUTPUT
MyRunnable,in run() method
*/
|
If we note output Thread-1 stayed in waiting state as there was no thread on same object to call notify().
First, lets discuss wait(long timeout) >
But, wait(long timeout) - Causes the current thread to wait until either another thread invokes the notify() or notifyAll() methods for this object, or a specified timeout time has elapsed.
So, in below program -
When wait(1000) is called on object - Thread enters from running to waiting state. Than even if notify() or notifyAll() is not called after timeout time has elapsed thread will go from waiting to runnable state.
class MyRunnable implements Runnable{
public void run(){
synchronized (this) {
System.out.println("MyRunnable,in run() method");
try {
this.wait(1000); //If no other thread holds object lock, thread
//will be notified after 1000 millisec.
System.out.println("Thread in waiting state, waiting for some "+
"other threads on same object to call notify() or notifyAll()");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class WaitWithParaMethod {
public static void main(String[] args) {
MyRunnable myRunnable=new MyRunnable();
Thread thread1=new Thread(myRunnable,"Thread-1");
thread1.start();
}
}
|
If we note output Thread-1 waited for 1000 millisecs to get notified and once 1000 millisecs elapsed it returned from waiting to runnable state.
What will happen if after 1000 millisecs object lock was holded by some other thread?
In that case thread will return from waiting to runnable state but wait for another thread to release object lock.
Please note : thread will wait for another thread just to release object lock, it won’t wait for thread to call notify() or notifyall(). Once object lock is available thread scheduler will put thread from runnable to running state at the discretion of the implementation.
RELATED LINKS>
Thread basics >
What is thread in java
Difference between Process and Thread in java
Implementing Threads in java by implementing Runnable interface and extending Thread class
Threads implement their own stack - demonstration using program and diagram
Differences between implementing Runnable interface and extending Thread class
Important Thread methods (salient features, usage with programs)>
Join() method
Yield() method in threads - 8 key features with programs
Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.
Daemon threads - 12 salient features of Daemon Thread
Differences between important thread methods >