473,231 Members | 1,578 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,231 software developers and data experts.

Is ArrayList in application object are thread safe?

Hi,

I want to keep a list of my visitors in an ArrayList which I place in the
application object like this:

Application("Visitors") = new ArrayList(); // The list of visitors

Then, each visitors update their own information in that list for every page
they visit.

Also, I add and remove visitors from the list as they come and go.

Can this cause concurrency situation and corrupt data? Do I need to lock the
Application("Visitors") each time I access it to add or to remove? Wouldn't
that be too much in term of performance? what is the best way to do this?

Thanks for any tips!

Stephane
Nov 19 '05 #1
5 2226
On Tue, 4 Oct 2005 10:45:03 -0700, "Stephane"
<St******@discussions.microsoft.com> wrote:

Can this cause concurrency situation and corrupt data? Do I need to lock the
Application("Visitors") each time I access it to add or to remove? Wouldn't
that be too much in term of performance? what is the best way to do this?


Yes, Stephane. You'll need to take care when modifying the list
itself. If the modifications are not done in a thread safe manner you
can end up with a corrupt list and strange runtime exceptions.

One approach is to use the thread safe ArrayList wrapper returned by
the Synchronized method. This makes your work easy, but may not be
what you need.

The synchronized wrapper makes individual operations thread safe, but
you might have code like this:

if(list.Count > 0)
Visitor v = list[0] as Visitor;

The problem with the above is that another thread might remove the
last visitor from the list between the time you check the Count
property and the time you retrieve the object with the [] indexer.
You'll get an exception.

The safest approach is to lock the list anytime you read or write:

lock(list.SyncRoot) {
// use the code above
}

.... and of course the safest approach is not going to be the most
performant approach.

Starting out, I would favor safety over performance, and run tests. In
most applications these locks won't kill performance. You can use
finer grained locks, or reader versus writer locks for more
throughput, but these add complexity.

If you do run into trouble I'd seriously consider storing the
information in a database. Relational databases excel at concurrency
management and fine grained locking.

Hope this helps,

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #2
Hi,

Thanks for your answer, it was helpful!

You think that managing visitors in a DB would be a more convenient way? I
thought it would be faster and simplier using application object.

Thanks,

Stephane
"Scott Allen" wrote:
On Tue, 4 Oct 2005 10:45:03 -0700, "Stephane"
<St******@discussions.microsoft.com> wrote:

Can this cause concurrency situation and corrupt data? Do I need to lock the
Application("Visitors") each time I access it to add or to remove? Wouldn't
that be too much in term of performance? what is the best way to do this?


Yes, Stephane. You'll need to take care when modifying the list
itself. If the modifications are not done in a thread safe manner you
can end up with a corrupt list and strange runtime exceptions.

One approach is to use the thread safe ArrayList wrapper returned by
the Synchronized method. This makes your work easy, but may not be
what you need.

The synchronized wrapper makes individual operations thread safe, but
you might have code like this:

if(list.Count > 0)
Visitor v = list[0] as Visitor;

The problem with the above is that another thread might remove the
last visitor from the list between the time you check the Count
property and the time you retrieve the object with the [] indexer.
You'll get an exception.

The safest approach is to lock the list anytime you read or write:

lock(list.SyncRoot) {
// use the code above
}

.... and of course the safest approach is not going to be the most
performant approach.

Starting out, I would favor safety over performance, and run tests. In
most applications these locks won't kill performance. You can use
finer grained locks, or reader versus writer locks for more
throughput, but these add complexity.

If you do run into trouble I'd seriously consider storing the
information in a database. Relational databases excel at concurrency
management and fine grained locking.

Hope this helps,

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #3
WJ

"Stephane" <St******@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...

You think that managing visitors in a DB would be a more convenient way ?
For stability, DB such as MS/SQL Server is recommended
I thought it would be faster and simplier using application object.


Yes, RAM is always the fastest regardless of what you do. Simply, because no
IO involved.

John
Nov 19 '05 #4
On Tue, 4 Oct 2005 11:54:00 -0700, "Stephane"
<St******@discussions.microsoft.com> wrote:
Hi,

Thanks for your answer, it was helpful!

You think that managing visitors in a DB would be a more convenient way? I
thought it would be faster and simplier using application object.


It might still be simpler to keep an object around in the Application
object, but remember if your web application resets you'll loose all
the information, that is another advantage to using a database.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #5
A hybrid approach might also be an option. Use the database as the
underlying store, and spin it up on application start, but from there,
do all your user actions could be in memory ... this reduces your load
on the database (making it more performant for other needs.)

Another fairly simple approach that might provide benefits is to create
your own class that wraps an ArrayList and internally use a
ReaderWriterLock object to provide finer grain locking around the
methods of ArrayList that you need. This could also have the benefit if
encapsulating the logic for writing updates to the database and spinning
itself up.

Stephane wrote:
Hi,

Thanks for your answer, it was helpful!

You think that managing visitors in a DB would be a more convenient way? I
thought it would be faster and simplier using application object.

Thanks,

Stephane
"Scott Allen" wrote:

On Tue, 4 Oct 2005 10:45:03 -0700, "Stephane"
<St******@discussions.microsoft.com> wrote:

Can this cause concurrency situation and corrupt data? Do I need to lock the
Application("Visitors") each time I access it to add or to remove? Wouldn't
that be too much in term of performance? what is the best way to do this?


Yes, Stephane. You'll need to take care when modifying the list
itself. If the modifications are not done in a thread safe manner you
can end up with a corrupt list and strange runtime exceptions.

One approach is to use the thread safe ArrayList wrapper returned by
the Synchronized method. This makes your work easy, but may not be
what you need.

The synchronized wrapper makes individual operations thread safe, but
you might have code like this:

if(list.Count > 0)
Visitor v = list[0] as Visitor;

The problem with the above is that another thread might remove the
last visitor from the list between the time you check the Count
property and the time you retrieve the object with the [] indexer.
You'll get an exception.

The safest approach is to lock the list anytime you read or write:

lock(list.SyncRoot) {
// use the code above
}

.... and of course the safest approach is not going to be the most
performant approach.

Starting out, I would favor safety over performance, and run tests. In
most applications these locks won't kill performance. You can use
finer grained locks, or reader versus writer locks for more
throughput, but these add complexity.

If you do run into trouble I'd seriously consider storing the
information in a database. Relational databases excel at concurrency
management and fine grained locking.

Hope this helps,

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #6

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

Similar topics

7
by: WildHare | last post by:
If I have a class and I add it to an ArrayList and then want to access that class using using the index operator (e.g. ArrayList) the ArrayList returns a type "Object". I can cast the return to...
0
by: Mike Grasso | last post by:
I've seen a few messages on this, but no responses. Here's what I found out PROBLEM: How do you use ArrayList.Synchronized to create thread-safe objects derived from ArrayList public class...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
14
by: Mike | last post by:
I had a question about threading and access to private class variables. I am developing a windows service which has a class inside of it which will receive various asynchronous calls to it via...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
1
by: KK | last post by:
Dear All I have a class whose methods are getting called from multiple threads in my application. For example class DataDistribution { private ArrayList datset; public DataDistribution() {...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
2
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...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.