notify and notifyAll 区别

Diffrence Between Notify & NotifyAll

Sathish Kumar Govindan 
Greenhorn 

Joined: Jan 23, 2007
Posts: 17 
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 

I like...
IntelliJ IDE  Java  MySQL Database
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 
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 

I like...
Eclipse IDE  Firefox Browser  Java
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 

I like...
Android  Hibernate  jQuery
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值