Thread-safe static methods/variables

S

S

Hi there,
I'm building an application that makes heavy use of centralized static
methods (and variables) and would just appreciate hearing someone grade my
understanding of the concept.

In probably common fashion, my static methods are typically in the DAL of my
web application. I don't have any concerns about the methods, but a lot of
collections (ArrayLists, custom collections inheriting from
System.Collections.CollectionBase, etc) are being created and passed out of
this layer. These are _not_ static collections, and my understanding is that
these are instantiated and passed out par norm, regardless of the fact that
the method itself is static. However, knowing that collections are
inherently thread-UNsafe, I still wonder about this and whether there are
exceptions to the rule or pitfalls I should be watching out for. It seems
that I shouldn't be worried about it in the capacity I've just described.
I'm pretty sure this is the case, but I'd rest easier if I could hear an
active confirmation of this..

Thanks for any advice,
S
 
P

Paul Glavich [MVP - ASP.NET]

You are ok as long as nothing is shared. The collections you mentioined,
while created/modified in a static method, are in fact created on the heap
as per normal and although the same method can be accessed by multiple users
(static method), the variables it creates are specific heap instances and
therefore not shared across any static methods unless they themselves are
static or thread statics. The DAAB (Data Access Application block) is based
on this premise.

So basically dont worry. All is good.
 
S

S

Thanks Paul!! I'm restin' easier!

Paul Glavich said:
You are ok as long as nothing is shared. The collections you mentioined,
while created/modified in a static method, are in fact created on the heap
as per normal and although the same method can be accessed by multiple users
(static method), the variables it creates are specific heap instances and
therefore not shared across any static methods unless they themselves are
static or thread statics. The DAAB (Data Access Application block) is based
on this premise.

So basically dont worry. All is good.

--
- Paul Glavich
Microsoft MVP - ASP.NET


of
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top