aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with static sychronized methods 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 "Doubt with static sychronized methods" Watch "Doubt with static sychronized methods" New topic
Author

Doubt with static sychronized methods

Yuan Du
Ranch Hand

Joined: Nov 20, 2008
Posts: 34
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Yuan Du
Ranch Hand

Joined: Nov 20, 2008
Posts: 34
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

Yup! You are right. The output would then be predictable...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with static sychronized methods
 
Similar Threads
Threads(Can someone please explain why the answer to the following questions are unpredictable)
confusion in join()
Lock on hidden and hiding static method.
Why this synchronization doesn't work
about threads, using locks of the objects in synchronized blocks