• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Marcus green's mock test

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question # 42 from Marcus Green

Which of the following statements about threading are true
1) You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on a method declared with the keyword synchronized
4) Thread scheduling algorithms are platform dependent
Ans : I believe 2 and 4 are right answers. But 3 is also given as right answer. How can a thread obtain a lock on a "method". Isn't is supposed to get lock on object of the method?
Any help is greatly appreciated.
 
Enthuware Software Support
Posts: 4599
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your reasoning is correct. Lock is associated with object not with method. When a thread tries to enter a synchronized method it first tries to accuire the lock of that object ( or class, for static sync. methods).
-Paul.

------------------
Get Certified, Guaranteed!
http://pages.about.com/jqplus
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Strictly speaking you cannot obtain a lock on a method but on the objects monitor. However practically what it means is that only one thread can execute that method (or any other synchronized nonstatic methods) at a time. So its a kind of lock
Also, obtaining an objects lock has no effect, untill and unless it has some synchronized method or block.So if we consider lock as a situation when other thread can not access that resource, then C is also true.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I belive both Paul and Mohit are correct. To be strictly accurate the lock is on the object not the method. However as it is the method that is affected it is easy (if not correct) to think of the method as getting the lock. I ought to change the wording to refer to the Object lock.
Marcus
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic