0

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

user3380123
  • 703
  • 1
  • 6
  • 14

3 Answers3

1

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.

Izaaz Yunus
  • 2,828
  • 1
  • 19
  • 28
0

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?

Community
  • 1
  • 1
LostBalloon
  • 1,608
  • 3
  • 15
  • 31
0

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:

  1. 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.
  2. 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.
  3. Now thread A gets processor time again and continues with the line return elementData(index). This will return null, as removing has overwritten that place with null.

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).

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66