• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Lock on hidden and hiding static method.

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronized static methods lock the class, not the instance ;-)
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic