Diffrence Between Notify & NotifyAll
Sathish Kumar Govindan Greenhorn
Joined: Jan 23, 2007 Posts: 17
|
posted Wednesday, February 27, 2008 17:30
|
Hello group,
I know that its a very lame question to ask experts like you but still i cannot help myself asking the question that "What is the Diffrence Between Notify & NotifyAll" method in the Object Class.
Well so far my search on the Internet reveals that 'notify()' method wakes up a single thread waiting on the object and passes the control of the monitor to it. So far so good and for 'notifyAll()' it says that its will wake up all the threads waiting on the object and will select a thread to pass control to it. Well as per me during that period the unselected thread will again go back to sleep in the JVM scheduler list and they will need yet another call to Notifty (or NotifyAll) in order to wake them up. So, as far as i see there is no diffrence between notify & notifyall as they both will result in waking up a single thread waiting on the Object.
If above assertions are indeed true than why to have two diffrent methods.
Thanks, Sathish Kumar.G |
SCJP 5.0, SCWCD JEE5
|
|
Nitesh Kant Bartender
Joined: Feb 25, 2007 Posts: 1629
|
posted Wednesday, February 27, 2008 18:51
|
Satish: 'notifyAll()' it says that its will wake up all the threads waiting on the object and will select a thread to pass control to it
It is partly correct, notifyAll() will notify all the threads and after that all the threads will contest for the monitor lock. The thread that gets the monitor lock will start the work and the rest will be waiting for the monitor lock. The above difference is pretty big as the threads waiting for monitor will not require a notify() call to start processing but will start processing as soon as they get the monitor lock. If you take the thread dump and see the state of the threads (although getting this dump at the correct time is somewhat tough), In case of notify() only one thread will get to the RUNNING state and other will be in WAIT state
In case of notifyAll() only one thread will get to the RUNNING state and other will be in BLOCKED state
|
apigee, a better way to API!
|
|
Sathish Kumar Govindan Greenhorn
Joined: Jan 23, 2007 Posts: 17
|
posted Thursday, February 28, 2008 00:25
|
Hi Nitesh , Thanks dude... now i am clear about the concept......... your answer is pretty cool
with thanks, G.sathish kumar. |
|
Rahul Sudip Bose Ranch Hand
Joined: Jan 21, 2011 Posts: 637
|
posted Monday, April 11, 2011 19:35:44
|
Nitesh Kant wrote: If you take the thread dump and see the state of the threads (although getting this dump at the correct time is somewhat tough)
What is the "thread dump" and what are its uses ? How can i get it ?
|
SCJP 6. Learning more now.
|
|
Anantha Sharma Ranch Hand
Joined: Sep 01, 2010 Posts: 42
|
posted Thursday, April 14, 2011 02:25:09
|
a thread dump is like the stacktrace but only specific to the thread, its state & where in the program its running, much like a program counter.
a thread dump is a snapshot of exactly what's executing at a moment in time.
you can view the thread dump by using an app called jConsole, this comes bundled with java it can be found in <<JAVA_HOME>>\bin\ folder.
refer to Sun's documentation on how to use this application.
This message was edited 1 time. Last update was at Thursday, April 14, 2011 02:26:49 by Anantha Sharma
|
相关推荐
- **IllegalMonitorStateException:** An `IllegalMonitorStateException` is thrown by the `wait()`, `notify()`, and `notifyAll()` methods if the current thread is not the owner of the object's monitor (i...
9. wait、notify和notifyAll方法: 这些方法提供了线程间的通信机制,可以用于线程之间的协调与等待/通知模式。 10. 并发对象导向编程: 章节介绍了如何使用并发构造、对象和并发、设计动力和模式。 11. 不可变性...
- `wait`, `notify` 和 `notifyAll` 方法用于线程间的通信。 2. **并发编程的各个方面**: - **封装与同步**:如何将对象的状态封装起来,并确保在多线程环境下正确同步对这些状态的访问。 - **死锁和冲突**:...
停等协议(Stop-and-Wait Protocol)是一种简单的数据传输协议,常用于计算机网络基础课程中讲解数据通信的基本概念。在该协议中,发送方每次发送一个数据帧后必须等待接收方的确认,才能发送下一个数据帧,从而确保...
#### `wait` 和 `notify`/`notifyAll` 的区别 - **`wait()`**:使当前线程进入等待状态,直到被其他线程唤醒。需要注意的是,`wait()` 必须在同步上下文中调用(即在 `synchronized` 块或方法中),并且会释放当前...
线程同步是多线程编程中的重要概念,包括了synchronized关键字、wait()、notify()和notifyAll()方法,以及volatile关键字和java.util.concurrent包中的工具,如Semaphore、CyclicBarrier和CountDownLatch等。...
Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are used incorrectly. - 虽然这些方法的错误使用会导致死锁,但这并不是唯一的原因。 - E. It is possible for a single-...
- wait使线程进入等待状态直到其他线程调用notify或notifyAll。 本篇Java面经中所涉及的知识点,都是Java校招面试中可能会涉及的关键考察点,理解这些知识点能够帮助求职者在面试过程中展现出扎实的基础和对Java...
wait()来自Object类,释放锁,用于让线程等待,可以被notify()或notifyAll()唤醒。 **notify()与notifyAll()** notify()唤醒一个等待同一对象锁的线程,而notifyAll()唤醒所有等待同一对象锁的线程。唤醒的线程需要...
这些机制在Object类中以wait(), notify()和notifyAll()方法的形式提供。这三个方法都是Java中所有对象的成员,因为它们定义在Object类中。 1. **wait()方法**: 当一个线程调用对象的wait()方法时,该线程会释放...
书中可能会讲解Thread类和Runnable接口,以及线程同步与通信的方法如synchronized关键字、wait()、notify()和notifyAll()。 3. **网络数据传输**:涉及InputStream和OutputStream家族,以及BufferedReader和...
一个线程在调用`wait()`后会释放锁并进入等待状态,直到其他线程调用`notify()`或`notifyAll()`唤醒它。 - 使用这些方法时必须在同步环境中(即`synchronized`代码块或方法内),否则会抛出`...
- **wait/notify/notifyAll**:用于线程间通信,控制线程的执行顺序。 - **synchronized关键字**:用于保证原⼦性和可见性,实现线程同步。 - **CAS(Compare And Swap)**:无锁编程技术,比较并交换,用于原子...
此外,可以使用wait()、notify()或notifyAll()方法进行线程间的通信,确保线程协作无误。 4. **避免阻塞**:尽量减少线程的阻塞时间,例如避免长时间的IO操作。可以使用异步回调或事件驱动模型来处理这些操作。 5....
本节重点讨论了如何利用`Object.wait`、`Object.notify`和`Object.notifyAll`等监控器方法来管理对象的状态,从而实现更高级别的同步。 #### 2.2.2 应用案例 通过实例演示了如何使用这些方法来控制线程的执行顺序,...
线程间的通信可以通过wait()、notify()和notifyAll()方法进行,但这些操作必须在同步块中进行,以避免死锁和竞态条件。另外,使用synchronized关键字可以确保线程安全,防止数据不一致。 实时编程则关注程序对时间...
- **wait/notify 和 notifyAll**:这些方法通常与 `synchronized` 关键字配合使用,用于线程间的通信。但是,在高并发场景下,可能会遇到死锁等问题,因此需要注意使用场景。 - **线程池**:`ThreadPoolExecutor` 是...
14. **线程同步方法**:wait、sleep、notify和notifyAll用于控制线程状态和通信。 15. **析构函数和虚函数**:析构函数在对象生命周期结束时自动调用,虚函数用于多态性,允许子类重写父类方法。 16. **Error与...
根据给定的信息,本文将详细解析“2024年秋招and春招—中威面试题”中涉及的关键IT知识点,主要包括Java框架的核心概念、Spring框架中的两大核心思想(IOC与AOP)、SpringMVC的工作流程、以及一些常见的编程问题。...
- **`wait/notify/notifyAll`**:用于同步多个线程的操作,确保某些条件满足后再继续执行。 - **`CountDownLatch`**:等待一组操作全部完成。 - **`CyclicBarrier`**:一组线程等待到达某个点后同时继续执行。 - **`...