469,631 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

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 8102

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Eric | last post: by
3 posts views Thread by Igor | last post: by
16 posts views Thread by Michael C# | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.