Threading and locking

  • Thread starter Thread starter xzoozx
  • Start date Start date
X

xzoozx

Object Instance "a" spawns multiple threads that act on Collection "p",
a property of "a" (not a static property of the class).

Do I have to synchronize (ie create a lock) on thread access on
Collection "p"?
 
Hi,

Collections are not thread safe as per se, some collections like Queue has
a sync method that assure concurrency safety.

You can use if you only "read" or iterate in them, if one thread add an
element and another thread is iterating on it the second thread will get an
exception.

I would sync the access anyway. just to be sure

Cheers,
 
Thanks for the answers, but I guess I wasn't too clear on the point of
my question. I'm assuming that the collection isn't thread-safe, but
since I'm working with the collection as an instance variable rather
than a static one, I was wondering if any operations on the instance
variable can be assumed to be synchronized and therefore thread-safe.
 
Thanks for the answers, but I guess I wasn't too clear on the point of
my question. I'm assuming that the collection isn't thread-safe, but
since I'm working with the collection as an instance variable rather
than a static one, I was wondering if any operations on the instance
variable can be assumed to be synchronized and therefore thread-safe.

No - if you've got multiple threads working with the same object that
the collection belongs to, there's no automatic synchronization going
on (thankfully - in situations where you don't need it, it would be a
performance killer).
 
Back
Top