Now we Know that array list is not synchronized and vector is.But how does this synchronizing make vector thread safe? An example that shows it would make it thread safe would be really helpful
-
1Do you know what `synchronized` is? – Sotirios Delimanolis Mar 22 '14 at 06:20
-
yes I know,its like a lock,if one thread is calling the synchronised method of the object no other thread with same object can call the other or the same method with synchronized keyword – user3380123 Mar 22 '14 at 06:23
-
So how does that apply here? – Sotirios Delimanolis Mar 22 '14 at 06:25
-
That's what I want to know – user3380123 Mar 22 '14 at 06:27
-
Say two threads wanted to set the value of the Vector element at position 2, which thread would succeed? Which value would the element at that position have? – Sotirios Delimanolis Mar 22 '14 at 06:28
-
Not only vector, synchronization is only for providing thread safe for any collection. – JDGuide Mar 22 '14 at 06:43
-
Note that just because the collection is thread-safe, that doesn't mean your code is. You can easily do non-thread-safe things with thread-safe collections. That's why they later realised that thread-safe collections aren't very useful. – user253751 Mar 22 '14 at 08:03
3 Answers
For the sake of simplicity, lets assume the following is the code to insert an element in an array.
void insert(int element)
{
elements[this.length] = element;
this.length++;
}
Now lets say we have 2 threads that try to insert an element in the array.
thread1.insert(5);
thread2.insert(7);
Consider the above method is not synchronized. So the following scenario will mess up the contents of the array.
Array contents = [1,2,3,4] and length = 4
Say, thread1 executes the first statement of the method and the execution is transferred to the second thread. Now the array contents become
Array contents = [1,2,3,4,5] but length is still 4
Now thread2 executes insert completely
Array contents = [1,2,3,4,7] and length = 5 *Error*
Now thread1 completes the method and now the final contents are
Array contents = [1,2,3,4,7] and length = 6
But if the insert method was defined to be synchronized, no two synchronized methods on the same object can run together. Hence when thread2 calls insert, it'll find that thread1 has a lock and it will wait for thread1 to complete. As a result, you wouldn't end up with stale data.
If synchronized is always safe, why do we even use unsynchronized methods?
The reason for this is, there's an extra overhead on the JVM to lock and unlock objects. So the trade-off here in using unsynchronized methods is that your processing time becomes a little better.
If you have a look at the Vector.java source code, you'll find that all the important methods are defined to be synchronized. Hence making them thread safe.

- 2,828
- 1
- 19
- 28
If you are not "synchronized" a common and frustrating issue you might face are race conditions (a real pain to debug!) more details here: What is a race condition?
Here is another thread that explains what the "synchronized" keyword does in java. I think the answer in this thread explains it pretty well and better than I would've myself: What does 'synchronized' mean?

- 1
- 1

- 1,608
- 3
- 15
- 31
As you already know, the synchronized keyword makes sure that only one thread can enter such a guarded code snippet, as only one thread can hold the object's Monitor.
Thread safety on the other hand is a much more complicated mechanism. It usually covers more than one object.
But back to Vector
. Not having the get
method synchronized could result in the following problem:
- Thread A enters the get method with an index equal to element count. The if condition is checked and proved to be ok. No exception is thrown.
- At this point thread B gets processor time. Thread A is set to waiting state. Thread B enters the remove method, which is completely run. Now an element is removed and the element count is decremented by 1.
- Now thread A gets processor time again and continues with the line
return elementData(index)
. This will returnnull
, as removing has overwritten that place withnull
.
So, synchronizing the methods make Vector indeed a little thread safe. But if you need more thread safety in terms of your program's requirements, you usually synchronize a whole code block with several calls to a vector (and also other objects).

- 20,273
- 6
- 37
- 66