Differences between wait() and join() methods in Java

The wait() and join() methods are used to pause the current thread. The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution.
wait() is mainly used for shared resources, a thread notifies other waiting thread when a resource becomes free. On the other hand join() is used for waiting a thread to die.

Similarities between wait() and join()

  • The method wait() and join() both are used to pause the current thread in Java.
  • Both wait() and join() can be interrupted by calling interrupt() method in Java.
  • Both wait() and join() are a non-static method.
  • Both wait() and join() are overloaded in Java. wait() and join() which without timeout as well as accepts a timeout parameter.

Difference between wait() and join() method

  • Most obvious difference, both are present different packages, the wait() method is declared in java.lang.Object class while join() is declared in java.lang.Thread class.
  • The wait() is used for inter-thread communication while the join() is used for adding sequencing between multiple threads, one thread starts execution after first thread execution finished.
  • We can start a waiting thread (went into this state by calling wait()) by using notify() and notifyAll() method but we can not break the waiting imposed by join without unless or interruption the thread on which join is called has execution finished.
  • One most important difference between wait() and join() that is wait() must be called from synchronized context i.e. synchronized block or method otherwise it will throw IllegalMonitorStateException but On the other hand, we can call join() method with and without synchronized context in Java.

  • Example of Join

    // Java program to explain the use of join()
      
    class A extends Thread {
      
        @Override
        public void run()
        {
            for (int i = 1; i <= 4; i++) {
                try {
                    Thread.sleep(100);
                }
                catch (Exception e) {
                    System.out.println(e);
                }
                System.out.print(i + " ");
            }
        }
    }
      
    class B extends Thread {
      
        @Override
        public void run()
        {
            for (char i = 'a'; i <= 'd'; i++) {
                try {
                    Thread.sleep(100);
                }
                catch (Exception e) {
                    System.out.println(e);
                }
                System.out.print(i + " ");
            }
        }
    }
      
    class GFG extends Thread {
      
        public static void main(String args[])
        {
            // creating two threads
            A a1 = new A();
            B b1 = new B();
      
            // starts second thread after when
            // first thread a1 is died.
            a1.start();
            try {
                a1.join();
            }
            catch (Exception e) {
                System.out.println(e);
            }
      
            // after thread a1 execution finished
            // then b1 thread start
            b1.start();
        }
    }

    Output:

    1 2 3 4 a b c d 
    


    Example of Wait

    // Java program to explain the
    // concept of Waiting a thread.
      
    class PNBCustomer {
      
        // let a initial amount is 9000
        int amount = 9000;
      
        // synchronized function because i wil use for
        // waiting thread. Here synchronized means inter-
        // thread communication
        synchronized void withdraw(int amount)
        {
            System.out.println("withdrawing...");
      
            // check if balance amount is less than withdraw
            // amount in this condition. deposit()  synchronized 
            // method call and deposit amount after that this 
            // thread again execute
            if (this.amount < amount) {
      
                System.out.println("Amount is not enough; waiting + 
                                               " for deposit...");
                try {
                    wait();
                }
                catch (Exception e) {
                }
            }
      
            System.out.println("after deposit, balance is available: " +
                                                         this.amount);
            this.amount -= amount;
            System.out.println("withdraw completed...");
            System.out.println("after Withdraw, balance is available: "
                                                         this.amount);
        }
      
        synchronized void deposit(int amount)
        {
            System.out.println("going to deposit...");
            this.amount += amount;
            System.out.println("deposit completed... ");
            notify();
        }
    }
      
    public class Wait {
      
        public static void main(String args[])
        {
            final PNBCustomer c = new PNBCustomer();
            // create two new thread and start
            // them simultaneously
            new Thread() {
                @Override
                public void run()
                {
                    c.withdraw(15000);
                }
            }.start();
            new Thread() {
                @Override
                public void run()
                {
                    c.deposit(10000);
                }
            }.start();
        }
    }

    Output:

    withdrawing...
    Amount is not enough; waiting for deposit...
    going to deposit...
    deposit completed... 
    after deposit, balance is available: 19000
    withdraw completed...
    after Withdraw, balance is available: 4000
    


    My Personal Notes arrow_drop_up

    Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.