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

Using ArrayList Synchronized

KK
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()
{
this.datset = new ArrayList();
}
public void SetData ( int x, IAssignable data )
{
ArrayList synchronizedList = ArrayList.Synchronized(this.dataset);
(synchronizedList [x] as IAssignable ).Assign ( data );
}
public IAssignable GetData( int x )
{
ArrayList synchronizedList = ArrayList.Synchronized(this.dataset);
return (this.synchronizedList [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 DataDistribution
{
private ArrayList datset;
public DataDistribution()
{
ArrayList tmpList = new ArrayList();
this.datset = ArrayList.Synchronized ( 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 8346

"KK" <kr*****@lucidindia.net> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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()
{
this.datset = new ArrayList();
}
public void SetData ( int x, IAssignable data )
{
ArrayList synchronizedList = ArrayList.Synchronized(this.dataset);
(synchronizedList [x] as IAssignable ).Assign ( data );
}
public IAssignable GetData( int x )
{
ArrayList synchronizedList = ArrayList.Synchronized(this.dataset);
return (this.synchronizedList [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 DataDistribution
{
private ArrayList datset;
public DataDistribution()
{
ArrayList tmpList = new ArrayList();
this.datset = ArrayList.Synchronized ( 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
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...
0
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...
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:...
3
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...
5
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...
16
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
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:...
3
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...
10
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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...

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.