Question on static methods

S

Simon Harvey

Hi all,

In my project I have made a number of helper methods static. As I understand
it, this will create the problem that multiple threads could access the
static method at the same time and interfere with one another.

My question is, for each static method, do I need to lock access to only one
call at a time? I've noticed that Microsofts Data Application block also
uses static methods for its data access calls - for example execute and
executeNonQuery etc. Should these methods be changed to only allow access by
a single thread at any given moment?


Perhaps there is an easier solution than this. A colleague of mine is
banging on about what a terrible performance hit this will cause but given
that the application is quite small I dont think that it will be much of a
problem.

Any suggestions on what to do would be very much appreciated

Thank you all

Simon
 
M

Marina

You only need to synchronize the method if it will be accessing some sort of
shared data - like always writing to the same file.

If the method is doing something like generically issuing SQL statements and
declares its variables locally, then you wouldn't need to.
 
H

Hans Kesting

Simon Harvey said:
Hi all,

In my project I have made a number of helper methods static. As I understand
it, this will create the problem that multiple threads could access the
static method at the same time and interfere with one another.

My question is, for each static method, do I need to lock access to only one
call at a time? I've noticed that Microsofts Data Application block also
uses static methods for its data access calls - for example execute and
executeNonQuery etc. Should these methods be changed to only allow access by
a single thread at any given moment?


Perhaps there is an easier solution than this. A colleague of mine is
banging on about what a terrible performance hit this will cause but given
that the application is quite small I dont think that it will be much of a
problem.

Any suggestions on what to do would be very much appreciated

Thank you all

Simon

If I understand it correctly, the concurrency should not be a problem
(or else we need to redesign a LOT!)
If you use only variables that are local to the method, there should be no
problem with concurrent threads.

Hans Kesting
 
K

Kevin Spencer

Only static data or methods which modify static data should be locked.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Simon Harvey

Hi All,

With regards to shared data, I have a Connection object and a command object
that is declared and used inside the static method. It is completely local.

Is this considered shared? I mean, is it shared by threads at the same time?

Also, is there a way to lock on a method rather than on a particular object.
Basically so you know that only one thread is in a method at a given moment?

Thanks for your help

Simon
 
K

Kevin Spencer

With regards to shared data, I have a Connection object and a command
object
that is declared and used inside the static method. It is completely local.

Is this considered shared? I mean, is it shared by threads at the same
time?

No. Objects which are declared inside a function are not a problem.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
R

Rick Spiewak

As long as you aren't using variables located outside of the static methods
except those passed in by reference, you should be fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top