• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with static sychronized methods

 
Yuan Du
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

Source: Examlab 3 Question 44


The answer is indeterminate result.

I don't know what is the different between invoking a static synchronized method and a synchronized method in run().
And what will happen if we redefine the static methods of the super class?
Could you please explain it?

Thanks,

Yuan
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are saying the opposite of what the code is doing. You will not redefine the method in the super class. Instead in this code the static method is redefined in the sub class. Look at this code



Since two different methods are called which lock on different objects, so both the methods can be executed concurrently by two threads. Method 1 locks on HighTop.class and Method 2 locks on LowTop.class
 
Yuan Du
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:You are saying the opposite of what the code is doing. You will not redefine the method in the super class. Instead in this code the static method is redefined in the sub class.


Hi Ankit,

You are right. The static method of super class can be redefined in subclasses.
If we don't redefine staticMethod() in LowTop, the result must be A0 A1 A2 A3 A4 A0 A1 A2 A3 A4. Am i right?

Thank you!

Yuan

 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup! You are right. The output would then be predictable...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic