简体   繁体   English

加入和等待之间的多线程区别,通知

[英]multiThreading difference between join and wait,notify

I have not worked in multithreading , what is the difference between join and wait ,notify method ?我没有在multithreading工作过,join 和 wait,notify 方法有什么区别? is the difference only limited to obtain lock and refrain other threads from accessing it or are there other use cases?区别仅限于获取lock并阻止其他threads访问它还是有其他用例?

Why should I go for wait and notify in multithreading when join can be used for completion of thread execution?join可以用于完成thread执行时,为什么我要在multithreading waitnotify

It would be helpful if any real time examples have been provided如果提供任何实时示例会有所帮助

The method join (of class Thread ) wait for a thread to die:方法join (类Thread )等待一个线程死亡:

Waits for this thread to die.等待这个线程死亡。

The methods wait , notify , notifyAll are not related to the end of execution of one thread. waitnotifynotifyAll方法与一个线程的执行结束无关。

The method wait :方法wait

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.使当前线程等待,直到另一个线程为此对象调用 notify() 方法或 notifyAll() 方法。

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

Methods notify and notifyAll are used to awake a sleeping thread:方法notifynotifyAll用于唤醒休眠线程:

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. waitnotify常见用途是访问共享资源。 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.当生产者创建资源时,它会通知(或 notifyAll)唤醒等待此资源的线程(或多个线程)。

A common use of join is blocking the main thread until a configuration thread has finished his activity before continuing. join 的一个常见用途是阻塞主线程,直到配置线程完成其活动,然后再继续。

join( ): Waits for this thread to die. join( ):等待这个线程结束。

The join method allows one thread to wait for the completion of another. join方法允许一个线程等待另一个线程的完成。 If t is a Thread object whose thread is currently executing,如果 t 是其线程当前正在执行的 Thread 对象,

t.join();

causes the current thread to pause execution until t's thread terminates.导致当前线程暂停执行,直到 t 的线程终止。

wait() : Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. wait() :使当前线程等待,直到另一个线程为此对象调用 notify() 方法或 notifyAll() 方法。

notify() : Wakes up a single thread that is waiting on this object's monitor. notify() :唤醒在此对象的监视器上等待的单个线程。 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. notifyAll() :唤醒在此对象监视器上等待的所有线程。 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() wait() 和 sleep() 的区别

Difference between Synchronized block with wait/notify and without them? 带等待/通知和不带它们的同步块之间的区别?

Examples:例子:

A simple scenario using wait() and notify() in java java中使用wait()和notify()的简单场景

work-with-wait-notify-and-notifyall 与等待通知和通知所有人一起工作

The join() method waits for a thread to die. join() 方法等待一个线程死亡。 While wait() and notify() are used for inter thread communication.而 wait() 和 notify() 用于线程间通信。 In other words these methods will block the thread until some condition is met.换句话说,这些方法将阻塞线程,直到满足某些条件。

Assume we have 2 threads running thread-1 & Thread-2.假设我们有 2 个线程运行线程 1 和线程 2。

If we are applying join on Thread-1, Then Thread-2 wants to wait till completion of Thread-2.如果我们在 Thread-1 上应用 join,那么 Thread-2 想要等到 Thread-2 完成。

Assume Thread-1 contains some thousands of lines, but Thread-2 wants to wait till completion of 1st 10 lines..假设 Thread-1 包含数千行,但 Thread-2 想等到第 10 行完成。

If we use join then the Thread-2 wants to wait till completion of Thread-1.如果我们使用 join 则 Thread-2 要等到 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.如果我们使用Wait 和Notify,则不需要等到Thread-1 完成,在Thread-1 的第10 行完成后,另一个线程可以恢复。

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.此实现使用以 this.isAlive 为条件的 this.wait 调用循环。 As a thread terminates the this.notifyAll method is invoked.当线程终止时,调用 this.notifyAll 方法。 It is recommended that applications not use wait, notify, or notifyAll on Thread instances.建议应用程序不要在 Thread 实例上使用 wait、notify 或 notifyAll。

The correct way to wait for a Thread to terminate (and clean up) is join() .等待Thread终止(和清理)的正确方法是join()

The documentation recommends you do not use wait() , notify() or notifyAll() on Thread instances.该文档建议您不要Thread实例上使用wait()notify()notifyAll()

IMHO, it was a mistake to add a monitor to java.lang.Object .恕我直言,将监视器添加到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.声明方法synchronized的能力也是一个错误。 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.它往往会延长关键部分,如果确实需要(不是),可以将其限制为某个类java.lang.Synchronizable (或将其作为接口实现的类),而不是使类层次结构膨胀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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