472,378 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

FYI: Using ArrayList.Synchronized on Derived Classes

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
{
}

// This throws an exception
MyList list = (MyList)ArrayList.Synchronized(new MyList)
ANSWER:
You can't. ArrayList.Synchronized returns a new ArrayList object.
Sorry.

THE (not so great) SOLUTION:
Instead of deriving from ArrayList, Wrap it.

public class MyList : IList
{
private ArrayList inner;
public MyList()
{
inner = ArrayList.Synchronized(new ArrayList);
}

// Implement the IList members using the inner ArrayList
public object this[int index]
{
get
{
return inner[index];
}
set
{
inner[index] = value;
}
}

You get the rest...
If there is a better solution, post it!
Nov 15 '05 #1
0 1096

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

Similar topics

0
by: John Lucas | last post by:
I have been reading many of the posts about determining which network card to use (in a machine with multiple nics), and determining whether or not that card is active. I'm hoping this will help...
1
by: Iulian Ionescu | last post by:
I have the following problem. I derived a class from ArrayList to provide some additional events I need. Then I derived 2 different classes from this one and each of them defines an indexer that...
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...
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:...
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() {...
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...
28
by: wanwan | last post by:
I wrote a class and have objects from this class added to an arraylist. When I retrieve the objects from the arraylist, should I use ctype or directcast to cast them back to the original class.?
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.