Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
hai zhang
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
rajashree ghatak
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that we all agree except for hai zhang.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic