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

Difference between wait() and join() method

1 2 3 4 a b c d 

Example of Wait

filter_none

edit
close

play_arrow

link
brightness_4
code

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

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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.





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.


Article Tags :
Practice Tags :