wait() Method in Java With Examples Last Updated : 06 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Inter-Thread communication is a way by which synchronized threads can communicate with each other using the methods namely wait(), notify() and notifyAll(). wait() method is a part of java.lang.Object class. When wait() method is called, the calling thread stops its execution until notify() or notifyAll() method is invoked by some other Thread. Syntax: public final void wait() throws InterruptedException Exceptions InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification.IllegalMonitorStateException - if the current thread is not the owner of the object's monitor. Working: In java, synchronized methods and blocks allow only one thread to acquire the lock on a resource at a time. So, when wait() method is called by a thread, then it gives up the lock on that resource and goes to sleep until some other thread enters the same monitor and invokes the notify() or notifyAll() method.Calling notify() wakes only one thread and calling notifyAll() wakes up all the threads on the same object. Calling both these methods does not give up the lock on the resource, rather its job is to wake up the threads that have been sent to the sleep state using wait() method.A big difference between sleep() method and wait() method is that sleep() method causes a thread to sleep for a specified amount of time while wait() causes the thread to sleep until notify() and notifyAll() are invoked. In the code mentioned below, we have created a class GunFight which contains a member variable bullets that is initialized to 40 and two methods fire() and reload(). The fire() method fires the number of bullets passed to it until the bullets become 0 and when bullets become 0 it invokes the wait() method which caused the calling thread to sleep and release the lock on the object while reload() method increased the bullets by 40 and invokes the notify() method which wakes up the waiting thread. Java // Java program to demonstrate the use of wait() method class GunFight { private int bullets = 40; // This method fires the number of bullets that are // passed it. When the bullet in magazine becomes zero, // it calls the wait() method and releases the lock. synchronized public void fire(int bulletsToBeFired) { for (int i = 1; i <= bulletsToBeFired; i++) { if (bullets == 0) { System.out.println(i - 1 + " bullets fired and " + bullets + " remains"); System.out.println( "Invoking the wait() method"); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "Continuing the fire after reloading"); } bullets--; } System.out.println( "The firing process is complete"); } // reload() increases the bullets by 40 everytime it is // invoked and calls the notify() method which wakes up // the thread that was sent to sleep using wait() inside // of fire() method synchronized public void reload() { System.out.println( "Reloading the magazine and resuming " + "the thread using notify()"); bullets += 40; notify(); } } public class WaitDemo extends Thread { public static void main(String[] args) { GunFight gf = new GunFight(); // Creating a new thread and invoking // our fire() method on it new Thread() { @Override public void run() { gf.fire(60); } }.start(); // Creating a new thread and invoking // our reload method on it new Thread() { @Override public void run() { gf.reload(); } }.start(); } } Output40 bullets fired and 0 remains Invoking the wait() method Reloading the magazine and resuming the thread using notify() Continuing the fire after reloading The firing process is complete Comment More infoAdvertise with us Next Article wait() Method in Java With Examples S shivamsingh00141 Follow Improve Article Tags : Java Java-Multithreading Practice Tags : Java Similar Reads Year until() Method in Java with Examples until() method of the Year class used to calculate the amount of time between two Year objects using TemporalUnit. The start and end points are this and the specified Year passed as a parameter. The result will be negative if the end is before the start. The calculation returns a whole number, repre 2 min read Period get() method in Java with Examples The get() method of Period class in Java is used to get the value of the requested unit(YEARS, MONTHS or DAYS) given in the argument from this Period. Syntax: public long get(TemporalUnit unit) Parameters: This method accepts a single parameter unit of type TemporalUnit which is the unit to get requ 2 min read TimeUnit sleep() method in Java with Examples The sleep() method of TimeUnit Class is used to performs a Thread.sleep using this time unit. This is a convenience method that sleeps time arguments into the form required by the Thread.sleep method. Syntax: public void sleep(long timeout) throws InterruptedException Parameters: This method accepts 2 min read Period plus() method in Java with Examples The plus() method of Period class in Java is used to add the given amount of period to the specified period. This functions operates separately on YEAR, MONTH and DAY. Note: Normalization is not performed. 12 months and 1 year are different. Syntax: public Period plus(TemporalAmount amountToAdd) Par 2 min read Period from() method in Java with Examples The from() method of Period class in Java is used to get an instance of Period from given temporal amount. This function obtains a period based on the amount given in argument. A TemporalAmount represents an amount of time, which may be date-based or time-based. Syntax: public static Period from(Tem 1 min read Like