File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lock on hidden and hiding static method.

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronized static methods lock the class, not the instance ;-)
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic