473,654 Members | 3,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is arraylist threadsafe?

Hi all,

I am having one arraylist which i want to share accross threads.

What i am doing is traversing through element of arraylist in both the
thread. Means just reading element of that arraylist.

So my question is do i need to use lock while traversing. As i am not
updating inserting or deleing any element of arraylust, so i think it
should not ask for locking.

Please help me asap.

thanks in advance.

Sep 12 '07 #1
4 2416
On Sep 12, 3:22 pm, archana <trialproduct2. ..@yahoo.comwro te:
I am having one arraylist which i want to share accross threads.

What i am doing is traversing through element of arraylist in both the
thread. Means just reading element of that arraylist.
That should be fine. If you were writing to the list, you'd have to
lock in all threads. Just for reading, you're okay.

Jon

Sep 12 '07 #2

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ o80g2000hse.goo glegroups.com.. .
On Sep 12, 3:22 pm, archana <trialproduct2. ..@yahoo.comwro te:
>I am having one arraylist which i want to share accross threads.

What i am doing is traversing through element of arraylist in both the
thread. Means just reading element of that arraylist.

That should be fine. If you were writing to the list, you'd have to
lock in all threads. Just for reading, you're okay.
Although, if you aren't writing (and specifically if you aren't inserting or
removing), then an array will be even better than ArrayList.
>
Jon

Sep 12 '07 #3
On Sep 12, 10:22 am, archana <trialproduct2. ..@yahoo.comwro te:
Hi all,

I am having one arraylist which i want to share accross threads.

What i am doing is traversing through element of arraylist in both the
thread. Means just reading element of that arraylist.

So my question is do i need to use lock while traversing. As i am not
updating inserting or deleing any element of arraylust, so i think it
should not ask for locking.

Please help me asap.

thanks in advance.
I would synchronize access while traversing the arraylist. You may
not need to modify the arraylist now but you, or someone else, might
need to in the future. Is a single thread populating the array? If
you use a lock you'll be able to make sure that the readers wait until
the ArrayList is filled before reading.

If the performance impact of synchronizing access to the arraylist is
too high then consider using a ReaderWriterLoc k. Since all of your
threads are readers they will be able to access the arraylist
concurrently.

Sep 12 '07 #4
Hi,

"Arnshea" <ar*****@gmail. comwrote in message
news:11******** **************@ r34g2000hsd.goo glegroups.com.. .
On Sep 12, 10:22 am, archana <trialproduct2. ..@yahoo.comwro te:
>Hi all,

I am having one arraylist which i want to share accross threads.

What i am doing is traversing through element of arraylist in both the
thread. Means just reading element of that arraylist.

So my question is do i need to use lock while traversing. As i am not
updating inserting or deleing any element of arraylust, so i think it
should not ask for locking.

Please help me asap.

thanks in advance.

I would synchronize access while traversing the arraylist. You may
not need to modify the arraylist now but you, or someone else, might
need to in the future. Is a single thread populating the array? If
you use a lock you'll be able to make sure that the readers wait until
the ArrayList is filled before reading.

If the performance impact of synchronizing access to the arraylist is
too high then consider using a ReaderWriterLoc k. Since all of your
threads are readers they will be able to access the arraylist
concurrently.
In cases where you only read from the list there is no need for sync.
Sep 12 '07 #5

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

Similar topics

0
2765
by: Jason Morehouse | last post by:
Hello, Just wondering if anyone is using the apache worker module with php? I've complied php5 with zend threadsafe support, and apache2 with the MPM worker module on a Linux box. Everything seems sweet. mysqli_thread_safe() reports true... anyone know if this configuration may include modules that aren't threadsafe? with-apxs2=/usr/local/apache2/bin/apxs with-gd enable-gd-native-ttf
0
1343
by: Stephanie Stowe | last post by:
Hi. I have been posting like a looney on an issue I am working on. I will reiterate the background since you all do not want to remember my issues! I have an ASP app (biggish). We are creating the first of many JSP apps. Until such time as we re-engineer our ASP apps for our new JSP / Java / Websphere environment, we need a session bridge. The services are always initiated from within ASP where they are authenticated and authorized....
9
3182
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it, like doing an modification, when no references are involved? I don't know enough about CLR) At the moment the whole: lock(anobject) {
3
1844
by: vooose | last post by:
Consider a class, ClassA that is used to perform a task using multiple threads...ie ClassA aObj = new ClassA(param1); Thread t = new Thread(new ThreadStart(aObj.DoStuff)); anArrayList.Add(aObj); The above may be called in many places, so the size of anArrayList grows.
2
2914
by: David | last post by:
I have a static method in the dll that looks like this public static int myStaticFunction(int input){ } My question is, should I use a mutex inside the function to ensure that the function is threadsafe? This is used for ASP.NET. I am asking this question without knowing how IIS works as a multi-threaded application. Does each websession get a thread? If so, if two users are calling the static function at the same time, do they
3
2173
by: Diffident | last post by:
Hello All, Following is a static method. Can you please tell me if this is threadsafe...why? If it is not threadsafe...why? Thank you. public static string GetCachedApplicationWideObject(string HashtableKey) { //Cache holds the hashtable
2
3452
by: Macca | last post by:
Hi, I need a data structure such as an Array/ArrayList/Generic List to hold multiple instances of a user derfined class. This array will be accessed across multiple threads. However I dont want to lock the array itself as this will cause poor performance due to the array being accessed frequently. If I make the User defined class threadsafe will this be enough to make
10
8498
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new Hashtable()); 2. create ArrayList aList = ArrayList.Synchronized(new ArrayList()); 3. create a string sList = ""; For 1 and 2, since the list is synced, many threads can directly
2
2737
by: =?Utf-8?B?UGhpbA==?= | last post by:
I have two threads that access an ArrayList. One thread will store new entries. The other thread will retrieve the entries and then remove them from the list. Is this a thread-safe operation or do I need to use the Synchronization system? Thanks, Phil
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.