473,614 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15719

"GIMME" <gi************ *******@yahoo.c om> wrote in message
news:3f******** *************** ***@posting.goo gle.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 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.prin tln(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(SomeMuta bleType 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******** *************@n ews.xs4all.nl.. .

"GIMME" <gi************ *******@yahoo.c om> wrote in message
news:3f******** *************** ***@posting.goo gle.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 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.co m (GIMME) wrote in message news:<3f******* *************** ****@posting.go ogle.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.co m (GIMME) wrote in message news:<3f******* *************** ****@posting.go ogle.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
2410
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 wonder if someone could give me some advice on this. Generally, the documentation suggests that queues or similar constructs should be used for thread inter-process comms. I've had a lot of success in doing that (generally by passing in the queue during the __init__ of the thread) and I can see...
3
1871
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 to handle memory allocation // ??would static member work here??: // static std::allocator<T> alloc;
8
2315
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. Commonly it occurs when more than one thread tries to lock a resource that another thread has already locked. A workaround is to use the Monitor object's TryEnter method, and pass a timeout value, if timeout occurs the method returns false. This pattern will save you no end of pain and...
4
1952
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 app-domains unloaded is the question. I have read somewhere in this forum that statics live till the asp.net process is recycled. Please clarify if someone can. Thanx! "David Jessee" wrote: > I'd be VERY careful about using a shared page member for state management. > its true that there is...
11
1908
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 applications with the use of static keywords" (http://support.microsoft.com/?id=893666) that discusses the possiblity of users seeing other users data if they access static methods at the same time because values are shared. Although I never used it, I noticed that some SqlHelper functions part of...
11
3814
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 is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
5
7117
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 reviews every now and then. There’s a static function in business logic class invoked by a Web multi-user application. Each request calls this static function passing in one unique ID, and XML serialized object (as out param), function deserializes XML to object, creates new instances of DB Access...
2
3645
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 appended to the end if a file with that name already exists. I know the writing of the single space is overkill, but I am surprised I cannot find an example of this floating around the web and newsgroups my understanding of 'os.open' and 'os.fdopen' is minimal ## start fragment
0
1824
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 prevent code in other threads from executing at the same time. For example: <?php $pointer = fopen ('./thread.lock', 'a+'); flock ($pointer, LOCK_EX);
0
8142
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4136
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.