Does using the built-in synchronized Vector block make any difference?

I was asked this question in an interview.

The vector is already synchronized. Would it make a difference to call it inside a synchronized block?

synchronized{ // Call the vector here } 

My answer is: there would be no difference except some loss of performance.

Is the answer correct?

+4
source share
2 answers

No, this is not entirely correct. Vector synchronized over the Vector instance itself, while the synchronized block is actually synchronized in the instance that contains Vector . The two methods for entering a synchronized block should first get the monitor associated with this , and then get the monitor associated with the Vector instance.

The case with the edge is that if one of the threads contains a monitor that the other requires (if you have other synchronized blocks), you may have a dead end.

However, given only the section of the code sent, the thread that the monitor first receives on this will be the first to perform the operation on the vector. In addition, the sequence of operations in a Vector instance can be performed by the first thread without any sequence of operations by the second thread; this is necessary if you want to perform an atomic sequence of operations with a Vector instance, which will not be the case in a regular synchronized Vector instance. To present this in pseudo-code, the sequence of operations in the two cases presented below will be different if the context switches between two or more threads executing the same block:

Case A

 synchronized { vector.add(a); vector.add(b); /* * a and b are always added to the vector in sequence. * If two threads execute this block, the vector will contain {a,b,a,b}. */ } 

Case B

 { vector.add(a); vector.add(b); /* * a and b need not be added to the vector in sequence. * If two threads execute this block, the vector will contain one of {a,b,a,b}, {a,a,b,b}.... */ } 
+12
source

I would say that your answer is incorrect. The fact that the vector is synchronized protects only the internal state of the vector. But most of the time you need to make your class thread safe.

Suppose you want to implement a container containing no more than 10 elements, and you use Vector to store these elements. The add method will look like this:

 public void add(Object item) { if (vector.size() == 10) { throw new TooManyItemsException(); } else { vector.add(10); } } 

But this method is not thread safe, and you must synchronize it to make it thread safe. So yes, calling a vector method inside a synchronized block matters.

A vector can also be part of a larger set of fields that need to be updated synchronously. In this case, even if the synchronized block calls the method only from the vector, it protects the entire state of the object.

If you need a completely technical answer: it matters because the synchronized block and the vector call are not synchronized on the same object and thus do not do the same.

+2
source

All Articles