This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes are static methods not thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "are static methods not thread safe?" Watch "are static methods not thread safe?" New topic
Author

are static methods not thread safe?

Ram Simhan
Greenhorn

Joined: Jul 17, 2002
Posts: 4
Guys,
Our Architect suggests static methods in java programs are not thread safe when they run in a servlet environment. So, we changed all static methods to instance methods and now the application crawls. I was wondering, if it is true that they're not thread safe.
Thanks
Jeya Ram
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I might be wrong, and people with more experience are welcome to speak up... but I don't think that static methods are *inherently* thread un-safe. It's always more important to consider what you are doing in a method and how it might impact things like database connections, class-level variables, etc.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Its not a question of static versus instance, it is a question of how many Threads and how many objects. In a servlet environment there can be lots of Threads and only one set of static variables and methods.
Therefore, if you are going to use static methods you must make them Threadsafe:
1. Using only local variables when the state of the variable may change.
2. Its ok to use static variables if:
- they don't change -OR-
- you use synchronized access methods
Are these objects you are creating very expensive to make?
Bill
Ram Simhan
Greenhorn

Joined: Jul 17, 2002
Posts: 4
Yes. They create large objects.I also would like to know, how exactly static methods get executed.
I appreciate your time.
Jaya Ram
Dave Dillon
Greenhorn

Joined: Nov 03, 2001
Posts: 3
Something like this...
public class ProviderServlet extends HttpServlet
{
public static void aStaticMethod ()
{
// do some stuff...
}
}
public class Client
{
public void aClientMethod ()
{
// This executes the static method...
ProviderServlet.aStaticMethod ();
}
}
Dave
Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
Static or Instance, none of the methods in a servlet are inherently thread-safe, unless you make them so, by using the 'synchronize' construct or by implementing the SingleThreadModle interface. I say this because even non-static methods of the servlet are subject to multiple threads executing them. What your architect said about 'static' methods not being thread-safe is applicable to stand alone java classes, but not to servlets.
-Bala
Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
Its not a question of static versus instance, it is a question of how many Threads and how many objects. In a servlet environment there can be lots of Threads and only one set of static variables and methods.

Bill,
I see what you are saying, but isn't it true that there will only be one set of 'instance' variables and methods in a serlet environment(and hence not thread-safe?), unless you implement the 'SingleThreadModel' interface? Please correct me if I am wrong.
-Bala.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Good grief, sometimes a post just gets to me, so pardon if I stop being my usual kind and patient self for a moment...
IF you are correctly interpreting his statement, and not overly simplifying it, then IMHO your architect is an idiot. If he really and truly made a blanket statement like that then he doesn't understand a blasted thing about either Java threading or how the Java virtual machine works.
JUST BECAUSE A METHOD IS STATIC DOES NOT MAKE IT NOT THREADSAFE. JUST BECAUSE A METHOD IS AN INSTANCE METHOD DOESN'T MAKE IT NOT THREADSAFE EITHER!
Think about what "threadsafe" means. It means that if multiple threads run through a particular method that there is no possibility of entering into one of the classic threading problems like:
(a) Deadlock
(b) Race conditions
(c) thread resource starvation (reader/writer conflicts like the Dining philosopher's problem)
There is something that MUST be said about all of these problems: THEY ALL RELY ON THE PRESENCE OF DATA SHARED AMONG THE DIFFERENT THREADS.
It doesn't matter SQUAT if your methods are instance methods or static methods, the key to writing threadsafe code is to make sure that either there is NO shared data (the easiest way to do things) or that if there is shared data that it is protected by a monitor (which, in Java is implemented through the synchronized keyword).
So analyze your code. Look for places where data is shared among the threads. Then either eliminate it or protect it. That's the way you make code threadsafe.
ALL OF YOU need to sit down and LEARN some things about threading in Java. Read Doug Lea's "Concurrent Programming in Java". Read Scott Oak's "Java Threads". Read a book on the Java Virtual Machine like Meyer's. Read a good book on operating system design like Tannenbaum or Silbershatz and Galvin for Gosh sakes and LEARN about threading in general. Don't go flying off the handle making outrageous statements like this...
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Ram Simhan
Greenhorn

Joined: Jul 17, 2002
Posts: 4
Thanks Kyle. I appreciate the detailed and well put response. I argued on the same lines as you did with our architect and he couldn't agree more.
Sincerely,
Jaya Ram
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
I see what you are saying, but isn't it true that there will only be one set of 'instance' variables and methods in a serlet environment(and hence not thread-safe?), unless you implement the 'SingleThreadModel' interface? Please correct me if I am wrong.

Right - only one set of instance variables and one set of static variables. BUT it all depends on how you use them. I frequently have instance variables that get set once on initialization and are not changed during normal operation. I may change them by using some administrator reset function.
If creating your large objects is slowing things down, some serious re-engineering is in order.
(admirable flame there Kyle! )
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: are static methods not thread safe?
 
Similar Threads
Static variable
Calling Helper class static methods from Struts action class.
static methods and threads
StringBuffer & StringBuilder and equals()
Mock Question about thread-safe