472,342 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

static methods and race conditions

One of my coworkers insists that one should never use
static methods because race conditions exist. Thinking
along the lines that all variable assignments are
assignments to static variables.

He's wrong. Right?

Two users in a web session could both access a static
method at the same time and never have any trouble.

Right?
Jul 17 '05 #1
5 15416

"GIMME" <gi*******************@yahoo.com> wrote in message
news:3f**************************@posting.google.c om...
One of my coworkers insists that one should never use
static methods because race conditions exist. Thinking
along the lines that all variable assignments are
assignments to static variables.

He's wrong. Right?

Two users in a web session could both access a static
method at the same time and never have any trouble.

Right?


Static or not has nothing to do with it. Race conditions can only occur in
multithread environments, such as in servlet code where the servlet
container could handle multiple requests through the same object instances
at the same time.

Accessing the same object from multiple threads simultaneously can be a
problem but does not have to be. If for example two threads perform
simultaneous access to an object without changing its state that would be
fine. If they do change the state and the state transition involves instable
intermediate states a problem could arise. To prevent such problems Java has
constructs like the synchronized keyword and wait/notify.

Silvio Bierman
Jul 17 '05 #2
class Problematic
{
static int x;

static void method(int x)
{
Problematic.x = x;
}
}

/*
This is problematic (in a multi-threaded environment) because, if two
threads call the method, there is access to
shared data (Problematic.x), and therefore, a race condition exists.
However, the simple generalization that "static methods have race
conditions" is clearly incorrect and is likely the result
of misinterpreting some document.
*/

class Fine
{
static void method(int x)
{
System.out.println(x);
}
}

/*
This is fine to call the method from multiple threads, since the
method-local x is placed on the method call stack
*/

class Problematic
{
static void method(SomeMutableType smt)
{
// change smt
}
}

/*
Again, there is access to (and altering of) shared data (the object referred
to that is passed when the method is called), and thus, a problem exists.
*/
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software

"Silvio Bierman" <sb******@idfix.nl> wrote in message
news:3f*********************@news.xs4all.nl...

"GIMME" <gi*******************@yahoo.com> wrote in message
news:3f**************************@posting.google.c om...
One of my coworkers insists that one should never use
static methods because race conditions exist. Thinking
along the lines that all variable assignments are
assignments to static variables.

He's wrong. Right?

Two users in a web session could both access a static
method at the same time and never have any trouble.

Right?
Static or not has nothing to do with it. Race conditions can only occur in
multithread environments, such as in servlet code where the servlet
container could handle multiple requests through the same object instances
at the same time.

Accessing the same object from multiple threads simultaneously can be a
problem but does not have to be. If for example two threads perform
simultaneous access to an object without changing its state that would be
fine. If they do change the state and the state transition involves

instable intermediate states a problem could arise. To prevent such problems Java has constructs like the synchronized keyword and wait/notify.

Silvio Bierman

Jul 17 '05 #3
Thanks Tony and Silvio.

Just to be sure ...

class IsThisOK
{
static String go( String A )
String a = A
return a;
}

}

Can to processes assign little a in a race condition
and mess things up for one of the users?
Jul 17 '05 #4
gi*******************@yahoo.com (GIMME) wrote in message news:<3f**************************@posting.google. com>...
Thanks Tony and Silvio.

Just to be sure ...

class IsThisOK
{
static String go( String A )
String a = A
return a;
}

}

Can to processes assign little a in a race condition
and mess things up for one of the users?

It depends on the context of how you use it--namely what is considered
an error.

Two threads could run this at exactly the same time a could be
reassigned by a different thread before the return statement is
executed by the first.

Assuming this would be problematic, there are two options:

1. Avoid assigning things in a static function if in a multithreaded
environment if it isn't really necessary. In other words, eliminate
the critical section.

2. Prevent multiple threads from using the critical section
concurrently. Use a monitor or some other mutex.

---
Jared Dykstra
http://www.bork.org/~jared
Jul 17 '05 #5
gi*******************@yahoo.com (GIMME) wrote in message news:<3f**************************@posting.google. com>...
One of my coworkers insists that one should never use
static methods because race conditions exist. Thinking
along the lines that all variable assignments are
assignments to static variables.

He's wrong. Right?

Two users in a web session could both access a static
method at the same time and never have any trouble.

Right?

Static methods in themselves are not a problem, unless they depend
upon data outside of the local scope. This is the same for regular
methods too.

In general the same race condition issues arise for static methods in
a class as for 'regular' methods in a single object instance. You need
to be just as careful calling a Class' static methods from two threads
as calling an object's regular methods from two threads (and not
necessarily the same method either!)
-FISH- ><>
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I...
3
by: Bernhard Kick | last post by:
Hi all, I saw this code in the book "Accelerated C++" (chapt 11, iirc): template <class T> class Vec { ... std::allocator<T> alloc; // object...
8
by: mk | last post by:
You probably suspect the answer, typically its 'yes' deadlock can occur in any multithreaded application. Even ones that employ static members. ...
4
by: abCSharp | last post by:
I understand that static variables have app-domain scope. Till the app-domain is in memory, the static variable will be in memory. When are the...
11
by: Dave | last post by:
I'm trying to understand the implications of using static methods and properties in asp.net so I found an article "Troubleshooting ASP.NET...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There...
5
by: WebMatrix | last post by:
Hello, It might seem like a stupid question to some. But I need to put this issue to rest once and for all, since it keeps coming up in code...
2
by: manuelg | last post by:
Here is a code fragment, where I am trying to copy a file, avoiding overwrites and race conditions. The filename gets a '02','03','04' etc...
0
by: moltendorf | last post by:
I've been trying to find a suitable method for preventing race conditions in my own code. Currently I'm using a file and the flock function to...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.