简体   繁体   中英

multiThreading difference between join and wait,notify

I have not worked in multithreading , what is the difference between join and wait ,notify method ? is the difference only limited to obtain lock and refrain other threads from accessing it or are there other use cases?

Why should I go for wait and notify in multithreading when join can be used for completion of thread execution?

It would be helpful if any real time examples have been provided

The method join (of class Thread ) wait for a thread to die:

Waits for this thread to die.

The methods wait , notify , notifyAll are not related to the end of execution of one thread.

The method wait :

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()

Methods notify and notifyAll are used to awake a sleeping thread:

Wakes up a single thread that is waiting on this object's monitor.


A common use of wait with notify is accessing a shared resource. When the resource is not available the consumer wait on the monitor. When the producer create the resource it notify (or notifyAll) to awake thread (or threads) waiting for this resource.

A common use of join is blocking the main thread until a configuration thread has finished his activity before continuing.

join( ): Waits for this thread to die.

The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,

t.join();

causes the current thread to pause execution until t's thread terminates.

wait() : Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

notify() : Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened.

notifyAll() :Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

Refer to below posts for more details.

Difference between wait() and sleep()

Difference between Synchronized block with wait/notify and without them?

Examples:

A simple scenario using wait() and notify() in java

work-with-wait-notify-and-notifyall

The join() method waits for a thread to die. While wait() and notify() are used for inter thread communication. In other words these methods will block the thread until some condition is met.

Assume we have 2 threads running thread-1 & Thread-2.

If we are applying join on Thread-1, Then Thread-2 wants to wait till completion of Thread-2.

Assume Thread-1 contains some thousands of lines, but Thread-2 wants to wait till completion of 1st 10 lines..

If we use join then the Thread-2 wants to wait till completion of Thread-1. If we use Wait and Notify then there is no need to wait till completion of Thread-1, After completion of 1st 10 lines of Thread-1 the other thread can resume.

public class JoinDrawBack {

    static int sum=0;
    public static void main(String a[]) throws InterruptedException {
        MyThread thread_1=new MyThread();
        thread_1.start();       //starting thread_1
        thread_1.join();       //applying join on Thread-1
        System.out.println(sum);   //thread-2 printing the sum
                                        //here thread-2 wants to wait till completion of run method,
                                       //when we use join then there is no need to wait such a long time.
    }}class MyThread extends Thread{
    public void run(){
        for(int i=0;i<10;i++){
            JoinDrawBack.sum=JoinDrawBack.sum+i;
        }

        ///thousands of lines
    }
}

This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

The correct way to wait for a Thread to terminate (and clean up) is join() .

The documentation recommends you do not use wait() , notify() or notifyAll() on Thread instances.

IMHO, it was a mistake to add a monitor to java.lang.Object . It's totally irrelevant to most classes, bloats implementations and results in inappropriate use.

The ability to declare methods synchronized was also a mistake. It tends to lengthen critical sections and if really needed (it isn't) could be restricted to some class java.lang.Synchronizable (or classes implementing that as an interface) rather than bloating the class hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM