H
HalcyonWild
Hi,
I wanted to know when should I synchronize a list. No, this is not the
old question regarding ArrayList Vs Vector. My doubts are more related
to synchronization ( whether a plain ArrayList or synchronized
Arraylist or Vector)
The common answer I find is that Vector or synchronizedList( new
ArrayList ) should be used when two or more threads are accessing the
contents of the List.
For example, consider following code in a Java class.
Vector vec = new Vector();
for (int i = 0; i < somenumber; i++)
{
//do something
vec.add(new Integer(i));
//do more
}
My thought is that whether you use any list, you still would have to
make the loop synchronized. Even if a vector or synchronized ArrayList
is used, another piece of code might just come in and modify that
vector (not run that loop, but just want to modify vec ) before the for
loop comes back again to that place. This is because, synchronized list
means when one thread is updating, another cannot. But if the first
thread is not updating, other freely can. This might create
inconsistent data in the List.
As far as I understand, the synchronization in the Vector is on the
instance itself. So before calling any method on the Vector instance, a
lock is obtained by runtime. Once method execution is over, the lock is
available to any other thread.
This is more possible in Servlets or JSP. ( EJB , I do not think so,
because, a separate instance is assigned to each thread calling a
method in the EJB ).
Another example, would be using a List as a member inside a singleton
class. If two threads request the instance of the singleton, both will
get the reference to the same instance of the class. Say the second
thread gets an index and has to retrieve the object at that index
position in the list, while the first thread is updating the vector. It
might turn out that the value retreived by the second thread might not
be what it expected. So I would anyway have to synchronize the methods
in the singleton, to get a lock on that instance before attempting to
call any method on that instance.
When would I use a List and when would I use synchronized List. Does it
really make any difference whether I use Vector or ArrayList when I
have to synchronize all access to the List. Please give any example to
help me understand the situation.
I have searched google and also I have searched previous posts. I could
not find the answer to this specific question so I am posting this. I
did find lot of stuff related to ArrayList Vs Vector, and which is
better , but not related to synchronization and Lists.
Thanks.
I wanted to know when should I synchronize a list. No, this is not the
old question regarding ArrayList Vs Vector. My doubts are more related
to synchronization ( whether a plain ArrayList or synchronized
Arraylist or Vector)
The common answer I find is that Vector or synchronizedList( new
ArrayList ) should be used when two or more threads are accessing the
contents of the List.
For example, consider following code in a Java class.
Vector vec = new Vector();
for (int i = 0; i < somenumber; i++)
{
//do something
vec.add(new Integer(i));
//do more
}
My thought is that whether you use any list, you still would have to
make the loop synchronized. Even if a vector or synchronized ArrayList
is used, another piece of code might just come in and modify that
vector (not run that loop, but just want to modify vec ) before the for
loop comes back again to that place. This is because, synchronized list
means when one thread is updating, another cannot. But if the first
thread is not updating, other freely can. This might create
inconsistent data in the List.
As far as I understand, the synchronization in the Vector is on the
instance itself. So before calling any method on the Vector instance, a
lock is obtained by runtime. Once method execution is over, the lock is
available to any other thread.
This is more possible in Servlets or JSP. ( EJB , I do not think so,
because, a separate instance is assigned to each thread calling a
method in the EJB ).
Another example, would be using a List as a member inside a singleton
class. If two threads request the instance of the singleton, both will
get the reference to the same instance of the class. Say the second
thread gets an index and has to retrieve the object at that index
position in the list, while the first thread is updating the vector. It
might turn out that the value retreived by the second thread might not
be what it expected. So I would anyway have to synchronize the methods
in the singleton, to get a lock on that instance before attempting to
call any method on that instance.
When would I use a List and when would I use synchronized List. Does it
really make any difference whether I use Vector or ArrayList when I
have to synchronize all access to the List. Please give any example to
help me understand the situation.
I have searched google and also I have searched previous posts. I could
not find the answer to this specific question so I am posting this. I
did find lot of stuff related to ArrayList Vs Vector, and which is
better , but not related to synchronization and Lists.
Thanks.