This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes What's the use of having a synchronized static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "What Watch "What New topic
Author

What's the use of having a synchronized static method?

Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
I undesrtand that the use of having a synchronized method is, no 2 threads will try to execute one method at a time and corrupt the state of an object. So synchronized is useful when it come to instance methods. But what's the use of having a synchronized static method?
Thanks
JEAN.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

It's the same reason. Why do you think there is a difference? Threads can execute static methods just as easily as instance methods.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Synchronizing static methods is useful when the method accesses a shared "static" resource, such as a static field, an external resource etc.

A static method synchronizes on the Class object of the class it is declared on (instead of "this").


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What's the use of having a synchronized static method?
 
Similar Threads
lock for instance method and class static method
Thread locks
how to lock my Method
Thread Vs. Runnable
ArrayList & Vector