aspose file tools*
The moose likes Threads and Synchronization and the fly likes some question about static methods and Thread safety , please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "some question about static methods and Thread safety , please" Watch "some question about static methods and Thread safety , please" New topic
Author

some question about static methods and Thread safety , please

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you for reading my post.
1-Are static methods thread safe ? I mean if i declare a method to be static , will it run in guarded mode ?
2-if static methods are thread safe , does JVM run them internally in Synchronized mode?

by static method i mean :

Thank you
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Static-ness and thread safety are unrelated concepts. The JVM runs them in synchronized mode only if they're declared to be synchronized, or part of a synchronized block. So a static method needs to take the same precautions a non-static method would take against concurrent access problems.


Ping & DNS - my free Android networking tools app
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In general what makes something unsafe is two threads locking or modifying a shared resource like an object, file, database or partner system. Your method didn't do that, so it's ok for now.

You could have a shared resource as a static varible, a parameter, a member variable on a singleton, anything you get from another method, anything you give to another method or something external to your code. I bet there are more ways I didn't think of just now. All this can make maintaining thread safety kind of tricky!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
raminaa,

Are you aware that static methods can be modified by the keyword "synchronized", and that the following two ways of making a static method synchronized are equivalent?


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: some question about static methods and Thread safety , please