permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes explanation needed 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 "explanation needed" Watch "explanation needed" New topic

explanation needed

krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
what is the concept of class specific locks
help by giving any example

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
When a thread accesses a static synchronized method of a class, then it acquires the class lock i.e., Test.class lock instead of Test's instance lock.

It will be better understood by this code....

Here you will get only one output. either main or child.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
class specific locks can be acquired by
1.synchronizing on static methods.
2.synchronizing on the static objects defined in the class.
3.Locking on external objects like System.out
4.Locking on Singleton Objects.
krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
thanks Nassem
I agree. Here's the link:
subject: explanation needed
It's not a secret anymore!