ArrayList.Synchronized lock both read and write/add, insert and remove ? Is sync needed even at race

  • Thread starter Thread starter Ryan Liu
  • Start date Start date
R

Ryan Liu

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?

For example in the master program, I have an ArrayList , each item in it is
a running thread. When the thread starts, it adds itself in, when the thread
ends, it removes itself from the master ArrayList.


Do I need synchronized version of ArrayList. Without synchronize, it can
always add itself in and remove itself, even at a race condition, right? It
will never remove wrong one right?

Thanks for clarify!
Ryan
 
Hi Vadym,

Thanks for the prompt reply.

I have a silly question, what is IMO, "IMO you have to sync access".

Best Wishes,
Ryan

Vadym Stetsyak said:
Hello, Ryan!

RL> What does ArrayList.Synchronized really do for an ArrayList?

When you use ArrayList.Synchronized static method it returs SyncArrayList
object. This class inherits from ArrayList
RL> Is that equal to add lock(this) for all its public methods and
RL> properties? Not just for Add()/Insert()/Remvoe()/Count, but also for
RL> Item (this[])?

Yes, but lock() is used on the object you've passed to Synchronized method

RL> Normally,do I need sync at all? Isn't that true without sync the
RL> program can run faster?

If your list is accessed from multiple thread then it is preferable to use
synchronized access to the list.
Yes, it is true that app will run faster as there is no lock overhead,
however you should not forget about synchronization when working with
multiple threads.
RL> For example in the master program, I have an ArrayList , each item in
RL> it is a running thread. When the thread starts, it adds itself in, when
RL> the thread ends, it removes itself from the master ArrayList.

RL> Do I need synchronized version of ArrayList. Without synchronize, it
RL> can always add itself in and remove itself, even at a race condition,
RL> right? It will never remove wrong one right?

IMO you have to sync access, as removal in ArrayList measn comparing
values in the loop. For the loop Count property if ArrayList is used,
without sync access Count value may be inconsistent.
 
| 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[])?

Yes. I would use my own lock instead and a normal array list. The reason
is because normally you want to do multiple operations inside the same lock,
such as Count, Add, Remove. Using synchronize would mean each of those
operations does a lock instead of locking once as you would do with your own
lock. MS has noticed this as well and has not continued with the
synchronize thing in their new collections. If multiple threads will access
the collection, you need a lock. HTH
 

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