473,806 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using ArrayList Synchronized

KK
Dear All
I have a class whose methods are getting called from multiple threads in my
application.
For example
class DataDistributio n
{
private ArrayList datset;
public DataDistributio n()
{
this.datset = new ArrayList();
}
public void SetData ( int x, IAssignable data )
{
ArrayList synchronizedLis t = ArrayList.Synch ronized(this.da taset);
(synchronizedLi st [x] as IAssignable ).Assign ( data );
}
public IAssignable GetData( int x )
{
ArrayList synchronizedLis t = ArrayList.Synch ronized(this.da taset);
return (this.synchroni zedList [x] as IAssignable ).;
}
}
These two methods are being called from two different threads.
I'm just planning to modify the above class definition as shown below to
avoid calling Synchronized on array list every time.
class DataDistributio n
{
private ArrayList datset;
public DataDistributio n()
{
ArrayList tmpList = new ArrayList();
this.datset = ArrayList.Synch ronized ( tmpList );
}
public void SetData ( int x, IAssignable data )
{
(this.datset [x] as IAssignable ).Assign ( data );
}
public IAssignable GetData( int x )
{
return (this.dataset[x] as IAssignable ).;
}
}

Can anybody suggest me what are the advantages & disadvantages with these
two approaches?
Tons of thanks in advance.

Regards
Krishna
Mar 14 '06 #1
1 8384

"KK" <kr*****@lucidi ndia.net> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Dear All
I have a class whose methods are getting called from multiple threads in
my application.
For example
class DataDistributio n
{
private ArrayList datset;
public DataDistributio n()
{
this.datset = new ArrayList();
}
public void SetData ( int x, IAssignable data )
{
ArrayList synchronizedLis t = ArrayList.Synch ronized(this.da taset);
(synchronizedLi st [x] as IAssignable ).Assign ( data );
}
public IAssignable GetData( int x )
{
ArrayList synchronizedLis t = ArrayList.Synch ronized(this.da taset);
return (this.synchroni zedList [x] as IAssignable ).;
}
}
These two methods are being called from two different threads.
I'm just planning to modify the above class definition as shown below to
avoid calling Synchronized on array list every time.
class DataDistributio n
{
private ArrayList datset;
public DataDistributio n()
{
ArrayList tmpList = new ArrayList();
this.datset = ArrayList.Synch ronized ( tmpList );
}
public void SetData ( int x, IAssignable data )
{
(this.datset [x] as IAssignable ).Assign ( data );
}
public IAssignable GetData( int x )
{
return (this.dataset[x] as IAssignable ).;
}
}

Can anybody suggest me what are the advantages & disadvantages with these
two approaches?
Tons of thanks in advance.


1:
+ Nothing
- You haven't even bothered trying to compile it
- It wouldn't work anyway because the two list wrappers will use different
monitors to "protect" the list

2:
+ At least the access to the ArrayList is thread safe.
- You haven't even bothered trying to compile it
- It still wouldn't work anyway because you never actually put anything in
the list

Assuming that at some point you did put some IAssignables in the list you
still have to deal with multiple threads calling the methods on the
IAssignable - this is unrelated to the thread safety of the access to the
list.
Mar 14 '06 #2

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

Similar topics

0
1167
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 MyList : ArrayList { }
0
1069
by: Hollywood | last post by:
I have a DataGrid that is bound to an ArrayList as a SynchArray. The ArrayList is updated by a thread. The thread locks the array list whenever it does an add/remove operation and then tells the CurrencyManager to do a Refresh(). However, the CurrencyManager throws a "index is not valid" excecption if objects are removed too fast from the ArrayList; which basically says the DataGrid is trying to draw the contents of the ArrayList throw...
10
3597
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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsicollectionclasssyncroottopic.asp) And I'm interested in locking an ArrayList during the entire enumeration, as shown in the example code. My problem is that I'm STILL...
3
661
by: Igor | last post by:
Hi. I have ArrayList shared by many threads, I synchronized it with mutex. Now I discovered in MSDN that ArrayList should be synchronized with lock() and SyncRoot. Is it a problem that I synchronized it with mutex and what is the difference? Thanks.
5
2246
by: Stephane | last post by:
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.
16
5314
by: Michael C# | last post by:
I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible, or do I need to bind to a DataTable? Thanks
2
1747
by: j3ko | last post by:
Hi, I'm trying to start a thread that constantly iterates through an arraylist of items that the main thread adds and removes from...how would I accomplish this? Here's the gist of what I have: (can anybody give me pointers on what i'm doing wrong?) Class SecondThread aList as new arraylist innerthread as new thread(addressof task)
3
2983
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also for Item (this)? Normally,do I need sync at all? Isn't that true without sync the program can run faster?
10
8514
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
0
9719
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
9597
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
10620
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...
0
10369
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10372
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,...
1
7650
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
6877
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();...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.