• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Locking a Object - Doubt

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LINK: http://www.danchisholm.net/july21/mybook/chapter17/exam2.html

Question 9
Which of the following allows a thread t1 to become the holder of the lock of object obj1.

a. By blocking on I/O
b. By entering a synchronized instance method of the obj1
c. By invoking the wait method on the object
d. By entering the body of a block that is synchronized on obj1
e. By entering a synchronized static method of the obj1
f. By invoking the notify method on obj1

The answer given is a,b,d,f. I am not able to justify on 'f'.
How can choice 'f' make t1 to be the holder of obj1. obj1.notify() will alert one thread and NOT all threads waiting for obj1. So, how can you say for sure that obj1 will be locked by t1 ONLY ???

Please explain! Thanks!
[ October 30, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe, Object Methods, wait(), notify(), and notifyall() need an object lock before they can be accessed. So choice f is true.

Can u justify me how 'a' is true and e is false.



Madhu
--------
Preparing for SJCP
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Choice e is false because ... we are talking about STATIC SYNCHRONIZED METHOD here !

Every class has its own java.lang.Class instancethat represents that class. It is the java.lang.Class instance whose lock is used to protect the SYNCHRONIZED STATIC METHODS of a class.

By entering a synchronized static method of the obj1, the lock on obj1 is released and lock is now on the java.lang.Class instance.

I am still on doubt in choice 'a' too! However, this is because, the question is not clear. Does the question talks about SURE statements or CAN BE statements, is my first doubt here !
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic