| Author |
Exam LAb Threads Problem
|
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
is it necessary to have wait,notify in synchronized block or method
|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
Yes. wait(), notify() and notifyAll() methods must be called withing synchronized context( synchronized block, synchronized method)..These methods are not in SCJP exam as per other ranchers comments. Serialization is also not in the exam it seems.
|
OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
|
wait is thrown and caught. But t.notifyAll() throws an IllegalMonitorStateException.
|
 |
Trivikram Kamat
Ranch Hand
Joined: Sep 26, 2010
Posts: 155
|
|
Rohit Ramachandran wrote:wait is thrown and caught
Actually, wait() is not an exception which can be thrown.
IllegalMonitorStateException is (also) thrown when wait() is called, as method is not synchronized.
But in that case, Ex-B is printed.
|
OCPJP6
|
 |
Trivikram Kamat
Ranch Hand
Joined: Sep 26, 2010
Posts: 155
|
|
mohitkumar gupta wrote:
is it necessary to have wait,notify in synchronized block or method
Yes, it's necessary.
Wait and Notify provides a means of communication between threads which are synchronized on the same object.
When a Thread calls wait() method in synchronized context, it releases the object lock and waits.
Invoking notify() method from other thread occupying that object lock wakes up the waiting thread.
|
 |
 |
|
|
subject: Exam LAb Threads Problem
|
|
|