gpt4 book ai didi

java - 多线程join和wait,notify的区别

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:44:59 26 4
gpt4 key购买 nike

我没有在多线程方面工作过,加入和等待、通知方法有什么区别?区别仅限于获取并阻止其他线程访问它还是有其他用例?

join 可用于完成 线程执行?

如果提供任何实时示例将会很有帮助

最佳答案

方法join (属于 Thread 类)等待线程结束:

Waits for this thread to die.

方法waitnotifynotifyAll与一个线程执行结束无关。

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

方法 notifynotifyAll 用于唤醒 hibernate 线程:

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


waitnotify 的常见用法是访问共享资源。当资源不可用时,消费者在监视器上等待。当生产者创建资源时,它会通知(或 notifyAll)唤醒等待该资源的线程。

join 的一个常见用途是阻塞主线程,直到配置线程在继续之前完成他的 Activity 。

关于java - 多线程join和wait,notify的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43867510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com