Hello,
I have a class with all static methods that is called by multiple
threads. I was wondering what effect that has on the competing threads.
Does Thread2 have to wait until Thread1 is done with the
StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate
its own copy of the class? Would that remove the waiting contention?
Thanks. 18 3291
That's the whole point of multithreading :) So unless there is a
mutual-exclusion lock in your method the second thread will not wait for the
first thread to finish executing the static method.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Frank Rizzo" <no**@none.com> wrote in message
news:eW**************@TK2MSFTNGP15.phx.gbl... Hello,
I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done with the StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
Thanks.
I am still not clear. Let's say the static method DoSomeTask creates a
connection object and gets some data, then destroys the connection.
Then let's say 2 threads call DoSomeTask. Will the second thread have
to wait for the first thread to complete?
Regards
Gabriel Lozano-Morán wrote: That's the whole point of multithreading :) So unless there is a mutual-exclusion lock in your method the second thread will not wait for the first thread to finish executing the static method.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:eW**************@TK2MSFTNGP15.phx.gbl...
Hello,
I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done with the StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
Thanks.
No the second thread will also create a connection object and get some data
while the first thread is still getting some data. The difference between a
static method and a non-static method is that the static method belongs to
the type where the non-static method belongs to an instance of a class.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Frank Rizzo" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl... I am still not clear. Let's say the static method DoSomeTask creates a connection object and gets some data, then destroys the connection. Then let's say 2 threads call DoSomeTask. Will the second thread have to wait for the first thread to complete?
Regards
Gabriel Lozano-Morán wrote: That's the whole point of multithreading :) So unless there is a mutual-exclusion lock in your method the second thread will not wait for the first thread to finish executing the static method.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:eW**************@TK2MSFTNGP15.phx.gbl...
Hello,
I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done with the StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
Thanks.
Gabriel Lozano-Morán wrote: No the second thread will also create a connection object and get some data while the first thread is still getting some data. The difference between a static method and a non-static method is that the static method belongs to the type where the non-static method belongs to an instance of a class.
Thank you. I am proceeding with the static method then. Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
I am still not clear. Let's say the static method DoSomeTask creates a connection object and gets some data, then destroys the connection. Then let's say 2 threads call DoSomeTask. Will the second thread have to wait for the first thread to complete?
Regards
Gabriel Lozano-Morán wrote:
That's the whole point of multithreading :) So unless there is a mutual-exclusion lock in your method the second thread will not wait for the first thread to finish executing the static method.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:eW**************@TK2MSFTNGP15.phx.gbl...
Hello,
I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done with the StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
Thanks.
If your entire class exists of static methods and properties consider
applying the Singleton design pattern.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Frank Rizzo" <no**@none.com> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl... Gabriel Lozano-Morán wrote: No the second thread will also create a connection object and get some data while the first thread is still getting some data. The difference between a static method and a non-static method is that the static method belongs to the type where the non-static method belongs to an instance of a class.
Thank you. I am proceeding with the static method then.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
I am still not clear. Let's say the static method DoSomeTask creates a connection object and gets some data, then destroys the connection. Then let's say 2 threads call DoSomeTask. Will the second thread have to wait for the first thread to complete?
Regards
Gabriel Lozano-Morán wrote:
That's the whole point of multithreading :) So unless there is a mutual-exclusion lock in your method the second thread will not wait for the first thread to finish executing the static method.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:eW**************@TK2MSFTNGP15.phx.gbl.. .
>Hello, > >I have a class with all static methods that is called by multiple >threads. I was wondering what effect that has on the competing threads. >Does Thread2 have to wait until Thread1 is done with the >StaticClass.Method1 before it can use it? > >What if I removed static methods and made all the threads instantiate >its own copy of the class? Would that remove the waiting contention? > >Thanks.
I think that your confusion (and ours) arises because you haven't
stated where you are storing your data. Threading problems don't crop
up based on whether you have static or instance methods. They crop up
based on whether the threads share data, or state.
If DoSomeTask creates a connection _and then stores that connection in
a static variable_ that is visible to other threads, then you'll have
contention problems and will have to introduce a lock. If, however,
DoSomeTask creates a connection and puts the reference to it in a local
variable (which would be on the stack), there is no interaction between
threads, because each thread has its own stack.
If your static class has static state as well, then you have to worry
about locks. If it has no state, there should be no threading problems
and no waiting.
I disagree. Unless there's a specific need for singleton, you shouldn't use
it just for the sake of using it. for example, it's perfectly fine to use a
class consist solely of static utility methods. In fact, I think singleton
is a poor choice here.
"Gabriel Lozano-Morán" wrote: If your entire class exists of static methods and properties consider applying the Singleton design pattern.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:Ou**************@TK2MSFTNGP12.phx.gbl... Gabriel Lozano-Morán wrote: No the second thread will also create a connection object and get some data while the first thread is still getting some data. The difference between a static method and a non-static method is that the static method belongs to the type where the non-static method belongs to an instance of a class.
Thank you. I am proceeding with the static method then.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
I am still not clear. Let's say the static method DoSomeTask creates a connection object and gets some data, then destroys the connection. Then let's say 2 threads call DoSomeTask. Will the second thread have to wait for the first thread to complete?
Regards
Gabriel Lozano-Morán wrote:
>That's the whole point of multithreading :) So unless there is a >mutual-exclusion lock in your method the second thread will not wait for >the first thread to finish executing the static method. > >Gabriel Lozano-Morán >Software Engineer >Sogeti > >"Frank Rizzo" <no**@none.com> wrote in message >news:eW**************@TK2MSFTNGP15.phx.gbl.. . > > >>Hello, >> >>I have a class with all static methods that is called by multiple >>threads. I was wondering what effect that has on the competing threads. >>Does Thread2 have to wait until Thread1 is done with the >>StaticClass.Method1 before it can use it? >> >>What if I removed static methods and made all the threads instantiate >>its own copy of the class? Would that remove the waiting contention? >> >>Thanks. > >
I agree that one shouldn't use a singleton just for the sake of using
it. However, when you end up with a static class with static state,
it's a pretty good indication that a singleton would be better.
As well, classes that do database operations may later come under the
re-engineering microscope if more that one data base needs to be
supported. You can't use polymorphism, interfaces, and the like using a
static class.
This particular case is a good one for a singleton. However, I agree
with Daniel that utility classes shouldn't be made singletons "just
because". (For a good example of a static class that should be a static
class, see "System.Math".)
"De gustibus et coloribus non est disputandum"
Let's just say that I would personally prefer using the singleton design
pattern over static members that can call other static members that can call
other static members, and so on.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:A3**********************************@microsof t.com... I disagree. Unless there's a specific need for singleton, you shouldn't use it just for the sake of using it. for example, it's perfectly fine to use a class consist solely of static utility methods. In fact, I think singleton is a poor choice here.
"Gabriel Lozano-Morán" wrote:
If your entire class exists of static methods and properties consider applying the Singleton design pattern.
Gabriel Lozano-Morán Software Engineer Sogeti
"Frank Rizzo" <no**@none.com> wrote in message news:Ou**************@TK2MSFTNGP12.phx.gbl... > Gabriel Lozano-Morán wrote: >> No the second thread will also create a connection object and get some >> data while the first thread is still getting some data. The difference >> between a static method and a non-static method is that the static >> method >> belongs to the type where the non-static method belongs to an instance >> of >> a class. > > Thank you. I am proceeding with the static method then. > >> >> Gabriel Lozano-Morán >> Software Engineer >> Sogeti >> >> "Frank Rizzo" <no**@none.com> wrote in message >> news:%2****************@TK2MSFTNGP14.phx.gbl... >> >>>I am still not clear. Let's say the static method DoSomeTask creates >>>a >>>connection object and gets some data, then destroys the connection. >>>Then >>>let's say 2 threads call DoSomeTask. Will the second thread have to >>>wait >>>for the first thread to complete? >>> >>>Regards >>> >>>Gabriel Lozano-Morán wrote: >>> >>>>That's the whole point of multithreading :) So unless there is a >>>>mutual-exclusion lock in your method the second thread will not wait >>>>for >>>>the first thread to finish executing the static method. >>>> >>>>Gabriel Lozano-Morán >>>>Software Engineer >>>>Sogeti >>>> >>>>"Frank Rizzo" <no**@none.com> wrote in message >>>>news:eW**************@TK2MSFTNGP15.phx.gbl.. . >>>> >>>> >>>>>Hello, >>>>> >>>>>I have a class with all static methods that is called by multiple >>>>>threads. I was wondering what effect that has on the competing >>>>>threads. >>>>>Does Thread2 have to wait until Thread1 is done with the >>>>>StaticClass.Method1 before it can use it? >>>>> >>>>>What if I removed static methods and made all the threads >>>>>instantiate >>>>>its own copy of the class? Would that remove the waiting >>>>>contention? >>>>> >>>>>Thanks. >>>> >>>> >>
Bruce Wood wrote: I think that your confusion (and ours) arises because you haven't stated where you are storing your data. Threading problems don't crop up based on whether you have static or instance methods. They crop up based on whether the threads share data, or state.
If DoSomeTask creates a connection _and then stores that connection in a static variable_ that is visible to other threads, then you'll have contention problems and will have to introduce a lock. If, however, DoSomeTask creates a connection and puts the reference to it in a local variable (which would be on the stack), there is no interaction between threads, because each thread has its own stack.
If your static class has static state as well, then you have to worry about locks. If it has no state, there should be no threading problems and no waiting.
That's correct - I am confused by what the threads actually use.
Let me give you an example. Let's say I have a static transaction
object. It is used by a non-static class method to insert some data.
If 2 threads were to have a go at this non-static method, would they
both use the static transaction object? And as a result, would there be
contention for that static transaction object?
Tbanks
Yes. If the data is static then there is only one of it, and two
threads attempting to access the same thing would require some sort of
locking, either within the implementation of the object or imposed by
the methods that use it.
Just so that you know, I'm no expert on multi-threading. There are
others here (such as Jon Skeet) who know far more than I do.
However, I can tell you that threading problems do not have to do with
instance methods vs static methods, but have to do with whether two
threads are going after the same piece of data at the same time.
(To illustrate further, there is no danger in two threads calling the
same instance method in the same object if that method never refers to
static variables and never refers to "this." anything--never looks at
static state or the object's state. Since the two threads aren't trying
to play with the same data items at the same time, no problems.)
In your case, if you have one shared transaction object, you could
easily run into the (classic) problem in which the two threads try to
both create the transaction object:
if (transaction == null)
{
transaction = new Transaction();
}
or something like that. If both threads run the "if" test at the same
time, they will both discover that there is no transaction object
available, and they will both create a new Transaction, the later one
clobbering the Tranasaction created by the earlier one.
Again, the problem isn't really that they're running the same code. The
problem is that they are testing / manipulating the same (shared) data
at the same time.
Thanks, Bruce. That's what I thought and this fully clears it up for
me. Back to instance methods & properties.
Bruce Wood wrote: Yes. If the data is static then there is only one of it, and two threads attempting to access the same thing would require some sort of locking, either within the implementation of the object or imposed by the methods that use it.
Just so that you know, I'm no expert on multi-threading. There are others here (such as Jon Skeet) who know far more than I do.
However, I can tell you that threading problems do not have to do with instance methods vs static methods, but have to do with whether two threads are going after the same piece of data at the same time.
(To illustrate further, there is no danger in two threads calling the same instance method in the same object if that method never refers to static variables and never refers to "this." anything--never looks at static state or the object's state. Since the two threads aren't trying to play with the same data items at the same time, no problems.)
In your case, if you have one shared transaction object, you could easily run into the (classic) problem in which the two threads try to both create the transaction object:
if (transaction == null) { transaction = new Transaction(); }
or something like that. If both threads run the "if" test at the same time, they will both discover that there is no transaction object available, and they will both create a new Transaction, the later one clobbering the Tranasaction created by the earlier one.
Again, the problem isn't really that they're running the same code. The problem is that they are testing / manipulating the same (shared) data at the same time.
Frank Rizzo wrote: Thanks, Bruce. That's what I thought and this fully clears it up for me. Back to instance methods & properties.
Just for the record, using only instance methods and properties is no
guarantee of thread safety. If two threads happen to be using the same
object (class instance), you'll still have the same problem.
Obviously, you can prevent that from ever happening (by making sure no two
threads get the same object), but if it ever did happen...
--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
So would you argue for the System.Math class to be reimplemented as a singleton?
Regards
Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk
"De gustibus et coloribus non est disputandum"
Let's just say that I would personally prefer using the singleton design
pattern over static members that can call other static members that can call
other static members, and so on.
Gabriel Lozano-Mor?n
Software Engineer
Sogeti
I think that the OP had the opposite impression: that using static
methods and static fields was a guarantee of thread safety. He now
knows that it's not.
If you really think about it a class containing nothing but static members
is already an implementation of the singleton design pattern but with more
limitations than a full blown implementation of the singleton.
Gabriel Lozano-Morán
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:ep*************@TK2MSFTNGP14.phx.gbl... So would you argue for the System.Math class to be reimplemented as a singleton?
Regards
Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk
"De gustibus et coloribus non est disputandum"
Let's just say that I would personally prefer using the singleton design pattern over static members that can call other static members that can call other static members, and so on.
Gabriel Lozano-Mor?n Software Engineer Sogeti
Got agree with Bruce here. I do not understand your point. What *exactly*
would be gained by making System.Math a singleton?
I personally use the singleton pattern when I need to cache system wide
data, and ensure only one copy of that data is available through a central
source.
"Gabriel Lozano-Morán" wrote: If you really think about it a class containing nothing but static members is already an implementation of the singleton design pattern but with more limitations than a full blown implementation of the singleton.
Gabriel Lozano-Morán
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in message news:ep*************@TK2MSFTNGP14.phx.gbl... So would you argue for the System.Math class to be reimplemented as a singleton?
Regards
Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk
"De gustibus et coloribus non est disputandum"
Let's just say that I would personally prefer using the singleton design pattern over static members that can call other static members that can call other static members, and so on.
Gabriel Lozano-Mor?n Software Engineer Sogeti
Got agree with Bruce here. I do not understand your point. What *exactly*
would be gained by making System.Math a singleton?
I personally use the singleton pattern when I need to cache system wide
data, and ensure only one copy of that data is available through a central
source.
"Gabriel Lozano-Morán" wrote: If you really think about it a class containing nothing but static members is already an implementation of the singleton design pattern but with more limitations than a full blown implementation of the singleton.
Gabriel Lozano-Morán
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in message news:ep*************@TK2MSFTNGP14.phx.gbl... So would you argue for the System.Math class to be reimplemented as a singleton?
Regards
Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk
"De gustibus et coloribus non est disputandum"
Let's just say that I would personally prefer using the singleton design pattern over static members that can call other static members that can call other static members, and so on.
Gabriel Lozano-Mor?n Software Engineer Sogeti
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Matthews |
last post by:
Hi,
I have three classes:
Category
Author
Publisher
Each of these classes stores its information into
a database table of <ID, text>. (They are fields
that have a foreign key.) There is...
|
by: Ray Ackley |
last post by:
I'm experiencing a threading problem that's really perplexing me. I
have a multithreaded application that reads car ECU information that's
sent over the serial port by the ECU and received by the...
|
by: mflanagan |
last post by:
I have unmanaged C++ program that will load a managed C++ dll and then
call a function in that dll. The managed C++ routine will call some C#
routines. The unmanaged C++ main program will make...
|
by: Sahil Malik [MVP] |
last post by:
So here's a rather simple question.
Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets just
say I...
|
by: Stephen Walch |
last post by:
Our application environment consists of three basic layers:
1. Third-party unmanaged DLLs that were written before the CLR was invented
and maintain a significant amount of information (including...
|
by: intrader |
last post by:
I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;
namespace ValidatorsLibrary
{
public class...
|
by: mark4asp |
last post by:
Q: Initialising and updating a class with only static members &
database dependency
I have a class with the following members:
public static List<ACISACIS_List;
static AssetClass() { //...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |