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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Locks" Watch "Locks" New topic
Author

Locks

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
These are the answers given
but i did'nt understand them
can anyone explain??
Locking is the main mechanism for restricting simultaneous thread access to specific java code. Which of the following statement(s) are true regarding locks / monitors?
a)One lock / monitor is associated with each Class
b)One lock / monitor is associated with each Synchronized block
c)One lock / monitor is associated with each Thread
d)One lock / monitor is associated with each Object
e)All of the above
a,b,d
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I understand that objects and classes have locks, not code blocks nor methods. When a synched method is executed, the lock for the object has to be obtained first.



For <CODE>static</CODE> synched methods, the lock for the class is grabbed instead.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
All are true, I think, whether the test writer realized it or not.
C is misleading - every Object has a lock, and all Threads are Objects, therefore every Thread has a lock. But no do all other objects, so that's not very informative. But it's still true.
As for B, every synchronized code block seeks the lock of the monitor object or class named in the synchronized statement, or with the current instance or class if none is named. Thus the synchronized block is also "associated" with a lock, but in a different way than the monitor is. But the language is vague, so this is valid.


"I'm not back." - Bill Harding, Twister
josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
How do u Obtain alock on any object?
Is it when i call a synchmethod thru the object that the
object gets the lock
It is not clear!!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes. If a code block says "synchronized (someObject)", then the JVM will obtain a lock on someObject. If the code block does not name an object, or if "synchronized" is part of a non-static method declaration, then the JVM gets a lock on "this", the current instance of the enclosing class. If a static method is synchronized, then the JVM obtains a lock on the enclosing class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Locks
 
Similar Threads
I neeed answers for these
Threads and object monitor
17.1 Locks
Thread Help!!!
lock for instance method and class static method