wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Lock on hidden and hiding static method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Lock on hidden and hiding static method." Watch "Lock on hidden and hiding static method." New topic
Author

Lock on hidden and hiding static method.

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
This is from Devaka Examlabe.



Here subclass is hiding the static method of superclass, so lock is being taken on two different class instance one on LowTop.class and other is HighTop.class. So result is unpredictable.

But if subclass does not hide static method than what will happen?



Doubt: Still lock would be taken on two different instances or lock of HighTop.class instance would be shared by two threads.
Here result is predictable or not?
[ December 16, 2008: Message edited by: Punit Singh ]

SCJP 6
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
Synchronized static methods lock the class, not the instance ;-)
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I know Jean, synchronized static method locks on class that is: YourClass.class, means on the class instance, and synchronized instance method locks on object, means on the object instance of the class.

A class has two types of instance, one is Object instance that is new YourClass(), and other is class instance that is YourClass.class that returns java.lang.Class instance of your class.
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
Well since your LowTop class doesn't technically inherit the method, it's the staticMethod() of HiTop that will be called by both run() mehods, hence locking the HiTop class.

the output will thus always be

A0 A1 A2 A3 A4 A0 A1 A2 A3 A4
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Lock on hidden and hiding static method.
 
Similar Threads
Doubt with static sychronized methods
Threads(Can someone please explain why the answer to the following questions are unpredictable)
Thread - The # between static synchronized and non static synchronized
Thread behavior with synchronized method
Why this synchronization doesn't work