This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Which of the following statements are true? [a] Each and every object has a lock [b] Synchronized is used to give access to more than one Thread to access a single resource. [c] wait()/notify() always need to call from try/catch block [d] notifyAll() method moves all the waiting objects to Ready state The answer is acd. I think it should be abd.
option b is not true. "sychronized" keyword is used to prevent all the threads from accessing a shared resource at the same time.only one thread can acquire the lock on the object at any point of time and other threads have to wait to enter the monitor.Synchronized method or block ensures this. HTH rajashree. [This message has been edited by rajashree ghatak (edited November 04, 2001).]
Originally posted by hai zhang: Synchronized is used to give access to more than one Thread to access a single resource. I think it should be abd.
Sounds like B is right.. But it is wrong. Becuase it is just saying more than one Thread can access a single resource. Of cuz they can access the resource but just not at the same time, they have to wait wait wait.. Mabye you see it that way. It is a bad question. C, it is wrong. Why do you think it is wrong?? ?_?
In option c...Only wait needs to be enclosed in a try/catch block...I don't think notify() and notifyAll() methods need the same treatment. Agreed that an llegalMonitorStateException is thrown if the current thread is not the owner of this object's monitor when the notify() / notifyAll() is called...but these 2 methods need not be included in a try/catch block. One way to word option (c) would be... The wait, notify() and notifyAll() methods need to be executed in a synchronized block, otherwise the call will result in an IllegalMonitorStateException : True or False". Answer : True. Any contrary opinions? Shyam
[This message has been edited by Shyamsundar Gururaj (edited November 04, 2001).]