A
Agustin Chernitsky
Hi guys. I have one very very big doubt on java threads.
Let's say I have a collection Array with lots of objects inside. These
objects inside must be accessed by n number of threads. Now, to get some
syncronization on the data (each object inside the collection holds
references to other obj), I have to make the following methods on the
collection Array Synchronized:
addObject
removeObject
So... when one thread is using addObject, no other thread can access the
collection obj. First doubt: is this ok?
now, lets say I want to read or retrieve some info from the obj inside the
collection array. I will use the following method:
getObject
Here is my biggest doubt:
Case 1: If I make the getObject method Syncronized, I will form a queue of
threads when each threads wants to read from the collection. In my
application, this will happen a lot (most of the time really). I will kill
the performance of my application if this is the case...
Case 2: If I don't synchronize the getObject method, what will happen?
A1: All threads will be able to read from the collection unless the
addObject or removeObjects are locking the collection?
A2: All threads will be able to read from the collection even if the
addObject or removeObjects are locking the collection? (I don't like this
one...)
Well... hope someone can clear this for me... Thanks so much!
Agus.
Let's say I have a collection Array with lots of objects inside. These
objects inside must be accessed by n number of threads. Now, to get some
syncronization on the data (each object inside the collection holds
references to other obj), I have to make the following methods on the
collection Array Synchronized:
addObject
removeObject
So... when one thread is using addObject, no other thread can access the
collection obj. First doubt: is this ok?
now, lets say I want to read or retrieve some info from the obj inside the
collection array. I will use the following method:
getObject
Here is my biggest doubt:
Case 1: If I make the getObject method Syncronized, I will form a queue of
threads when each threads wants to read from the collection. In my
application, this will happen a lot (most of the time really). I will kill
the performance of my application if this is the case...
Case 2: If I don't synchronize the getObject method, what will happen?
A1: All threads will be able to read from the collection unless the
addObject or removeObjects are locking the collection?
A2: All threads will be able to read from the collection even if the
addObject or removeObjects are locking the collection? (I don't like this
one...)
Well... hope someone can clear this for me... Thanks so much!
Agus.