Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam LAb Threads Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is it necessary to have wait,notify in synchronized block or method
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait is thrown and caught. But t.notifyAll() throws an IllegalMonitorStateException.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic