473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is a readonly collection thread safe

Hi guys

I have in my application many collections (HashTable and
ArrayList)loaded with configuration data, that means they are loaded
at startup and then they are accessed only for read-only (items are
only retrieved, never added, removed or replaced).
To be thread-safe after startup do I need to made them Synchronized
(and so slower), or are they thread safe in such case ?

Thanks in advance
David
Jul 21 '05 #1
2 5165
Hi David,

I'd still recommend sticking to the safe way. As an alternative, introduce a
simple data holder class like this:

public class AppSettings
{
private int _setting1;
private string _setting2;

public AppSettings()
{
// Reads settings from some permanent storage and populates _setting1
and _setting2
}

public int Setting1
{
get
{
return _setting1;
}
}

public string Setting2
{
get
{
return _setting2;
}
}
}

and instantiate it on start-up and then safely access this data holder class
without any synchronization. You will most likely wish to turn this class
into a singleton - and that would be right decision - I have omitted any
singleton support for the sake of brevity.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"David" <pi***@hotmail.com> wrote in message
news:9c**************************@posting.google.c om...
Hi guys

I have in my application many collections (HashTable and
ArrayList)loaded with configuration data, that means they are loaded
at startup and then they are accessed only for read-only (items are
only retrieved, never added, removed or replaced).
To be thread-safe after startup do I need to made them Synchronized
(and so slower), or are they thread safe in such case ?

Thanks in advance
David


Jul 21 '05 #2
MSDN:
"By default, Collections classes are generally not thread safe. Multiple
readers can safely read the collection; however, any modification to the
collection produces undefined results for all threads that access the
collection, including the reader threads."

So, as long as you only read the collections, it's ok. Generally the "thread
safety" is defined by the synchronization of threads when accessing a common
resource, producing/consuming in the same time, but as long as no one
modifies it it's ok.

Anyhow Hashtable is thread safe.
--
Horatiu Ripa

"David" <pi***@hotmail.com> wrote in message
news:9c**************************@posting.google.c om...
Hi guys

I have in my application many collections (HashTable and
ArrayList)loaded with configuration data, that means they are loaded
at startup and then they are accessed only for read-only (items are
only retrieved, never added, removed or replaced).
To be thread-safe after startup do I need to made them Synchronized
(and so slower), or are they thread safe in such case ?

Thanks in advance
David

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
3
by: BoloBaby | last post by:
All, I believe I am having a threading problem. Class "BELights" is part of a larger DLL that is used by my main application. A user control (of type BESeat) within the main application raises...
0
by: fred | last post by:
I need some help in trying to understand how to make myCollection (inherited from CollectionBase) multithread safe. Taking my implementation of the Add Sub and a readonly property Item. Public...
11
by: Eric | last post by:
I have a VB.net dll project with a class that is a singleton. I've been using this in winform apps without any problems. I would like to use this same dll in a web form project but my singleton...
2
by: David | last post by:
Hi guys I have in my application many collections (HashTable and ArrayList)loaded with configuration data, that means they are loaded at startup and then they are accessed only for read-only...
7
by: Alexander Walker | last post by:
Hello I want to get the value of a property of a control from a thread other than the thread the control was created on, as far as I can see this is not the same as invoking an operation on a...
8
by: Senna | last post by:
Hi I have some questions about this code, http://code.doria.se/?c=18. 1. Will the Adapter class be thread-safe? 2. In the #region Way 1 I use lock(_lockobj) every time I go about and do...
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
12
by: David | last post by:
Below are three classes for a console application. If put into three separate files, the sub main() will launch multiple threads adding and removing the same value. At the end we expect the value...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...
0
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...

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.