File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus green's mock test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus green Watch "Marcus green New topic

Marcus green's mock test

Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
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.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
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).

Get Certified, Guaranteed!

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243

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.
Marcus Green
arch rival

Joined: Sep 14, 1999
Posts: 2813
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.

SCWCD: Online Course, 50,000+ words and 200+ questions
I agree. Here's the link:
subject: Marcus green's mock test
It's not a secret anymore!