Using ArrayList Synchronized

  • Thread starter Thread starter KK
  • Start date Start date
K

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
 
KK said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top