473,404 Members | 2,170 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,404 software developers and data experts.

Threadsafe access to ArrayList

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
Jun 27 '08 #1
2 2728
On May 14, 9:19*am, Phil <phi...@sbcglobal.netwrote:
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
Hi,

No, it's not.
You should use a Queue (or Stack) instead of an ArrayList. Queue has
a Synchronizedn method that you can use to make it thread safe.
Jun 27 '08 #2
There is an alternative,
you can create a global(which is accessible to both threads)
synchronization object which can simply be an variable of type Object

Then in each thread use this

//global or class level
Object ArraySyncObject;

thread1:

while(...)
{
lock(ArraySyncObject)
{
//add entryies here
}
}
-----------

thread2:
while(...)
{
lock(ArraySyncObject)
{
//retreive entries
//remove entries
}
}

This is just an alternative. So, make sure that you don't do much
processing in the lock block, as this will block the other thread from
execution.

Regards,
Ashutosh Bhawasinka

lock
Phil wrote:
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
Jun 27 '08 #3

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

Similar topics

0
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...
1
by: Matthias S. | last post by:
Hi, I'm trying to provide the user of my class access to the objects in a collection without allowing him to modify (add/remove/clear items) the collection. I thought I can do it by providing a...
9
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,...
3
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...
1
by: Gustyn | last post by:
I have line of code that was originally written prior to .NET 2.0 as follows: Foreach int i in Items. The problem is it keeps throughing an exception about unsafe thread access. I have read...
2
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...
6
by: Pavel Maly | last post by:
Hi, how do I access values in an ArrayList which is a part of another ArrayList? I know I can define a class and then it is quite simple, but this is just an auxiliary application to compute some...
4
by: archana | last post by:
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...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
How do I handle a generic collection to be sent from the server to the client if that collection must be accessed threadsafe ? my server object must be singleton. my idea of the singleton is:...
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
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...
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
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.