top of page

Difference Between Wait() and join() methods in Java?

Wait() Method

wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor.


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.

Example:

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();
    }
}


Join() Method

The join() method in Java is provided by the java.lang.Thread class that permits one thread to wait until the other thread to finish its execution. When there are more than one thread invoking the join() method, then it leads to overloading on the join() method that permits the developer or programmer to mention the waiting period. However, similar to the sleep() method in Java, the join() method is also dependent on the operating system for the timing, so we should not assume that the join() method waits equal to the time we mention in the parameters. The following are the three overloaded join() methods.


Syntax:

public final void join() throws InterruptedException

Example:

public class JoinTest extends Thread 
{
      public void run() 
      {
            for(int i=1; i <= 3; i++) 
            {
                 try 
                 {
                       Thread.sleep(1000);
                 } 
                 catch(Exception e) 
                 {
                       System.out.println(e);
                 }
                 System.out.println("TutorialsPoint "+ i);
            }
      }
      public static void main(String args[]) 
      {
            JoinTest t1 = new JoinTest();
            JoinTest t2 = new JoinTest();
            JoinTest t3 = new JoinTest();     
            t1.start();
            try 
            {         
                  t1.join(); // calling join() method      
            } 
            catch(Exception e) 
            {
                  System.out.println(e);
            }     
            t2.start();
            t3.start();
      }
}

Similarities Between Wait() and Join()

Overloaded Methods

Both wait() and join() are overloaded in Java. You can find a version of both the wait() and join() which accepts a timeout parameter.

Interruption

Both wait() and join() can be interrupted by calling interrupt() method in Java. This means you can handle the interrupt by catching InterrruptedException in Java.

Non-static Methods

Both wait() and join() are non-static methods and cannot be called from the static context.

Pausing a Thread

In wait(), the thread which calls the wait() method goes into waiting for the state. In Join(), the thread which calls the join() method goes into waiting for the state until the thread on which join has been called finishes its execution.



Difference Between Wait() and Join()


Wait()

Join()

Thread vs Object

It is declared and defined in the object class

It is defined in the Thread Class

Synchronized Context

It must be called from Synchronized content; Synchronized method otherwise it will throw IllegalMonitorStateException but there is no such requirement for calling method in java

You can call the join() method with and without synchronized context in java.

Release of lock

The Thread which calls method release any lock held for the object on which the wait() is called.

The Calling join() method doesn't release any monitor or lock.

Purpose

Wait() is used for inter-thread communication

Join() is used for adding sequencing between multiple threads like one thread starts execution after the first thread finishes its execution.

Notification

You can awake a thread waiting by calling the wait() method of the object class by using notify() and notifyAll() method

you can not break the waiting imposed by join without interruption or unless the thread on which join is called has finished execution.



The Tech Platform

0 comments
bottom of page