aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

hai zhang
Greenhorn

Joined: Oct 30, 2001
Posts: 11
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.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
The purpose of the keyword "synchronized" is to prevent more than one thread from accessing a single resource simultaneously.

Why do you think 'c' is incorrect?

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
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).]
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
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?? ?_?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
It seems that we all agree except for hai zhang.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread