By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Question on static methods

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #2

P: n/a

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message news:uX**************@TK2MSFTNGP09.phx.gbl...
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
Nov 18 '05 #3

P: n/a
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.

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #4

P: n/a
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
Nov 18 '05 #5

P: n/a
> 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.

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:#S**************@tk2msftngp13.phx.gbl... 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

Nov 18 '05 #6

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

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.