
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When can we call wait() and wait(long) methods of a Thread in Java?
This article will discuss the wait() and wait(long timeout) methods, along with a suitable example that explains when to call these methods in a thread in Java.
The wait() Method
In Java, the wait() method is used in multithreading and causes the current thread to "wait" until another thread calls notify() or notifyAll() on the same object. When the wait() method is called, the thread moves from the running to the waiting state and resumes only when notified, if not a deadlock will be formed.
Note: Call the wait() method only when the thread should wait without any time limit.
Following is the syntax of the Java Thread wait() method -
public final void wait() throws InterruptedException
Example
The following example shows a real-time use case of when to call the wait() method -
class MyRunnable implements Runnable { public void run() { synchronized(this) { System.out.println("In run() method"); try { this.wait(); System.out.println("Thread in waiting state, waiting for some other threads on same object to call notify() or notifyAll()"); } catch (InterruptedException ie) { ie.printStackTrace(); } } } } public class WaitMethodWithoutParameterTest { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable, "Thread-1"); thread.start(); } }
Following is the output of the above program -
In run() method
The wait(long timeout) Method
In Java, the 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(long timeout) is called on an object, the thread enters from running to waiting state, even if notify() or notifyAll() is not called after the timeout time has elapsed thread will go from waiting to runnable state.
Note: Call the wait(long timeout) method only when the thread should wait till the given time limit.
Following is the syntax of the Java Thread wait(long timeout) method -
public final void wait(long timeout) throws InterruptedException
Here, the timeout specifies the maximum time (in milliseconds) that the current thread should wait.
Example
class MyRunnable implements Runnable { public void run() { synchronized(this) { System.out.println("In run() method"); try { this.wait(1000); System.out.println("Thread in waiting state, waiting for some other threads on same object to call notify() or notifyAll()"); } catch (InterruptedException ie) { ie.printStackTrace(); } } } } public class WaitMethodWithParameterTest { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable, "Thread-1"); thread.start(); } }
Following is the output of the above program -
In run() method Thread in waiting state, waiting for some other threads on same object to call notify() or notifyAll()
Difference between wait() and wait(long timeout) Method
Below are the difference between wait() and wait(long timeout) method -
- The wait() method waits indefinitely until notified, whereas the wait(long timeout) method waits until notified or until the specified timeout expires.
- The wait() method does not accept any timeout, whereas the wait(long timeout) method accepts a timeout value in milliseconds.
- The wait() method resumes only when another thread calls notify() or notifyAll(), whereas the wait(long timeout) method resumes either when notified or when the timeout is reached.