File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Vector Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector Synchronization" Watch "Vector Synchronization" New topic
Author

Vector Synchronization

ankit maini
Greenhorn

Joined: Feb 08, 2011
Posts: 25

I have read about vector that it is synchronized means its methods are synchronized, In the give below example i am adding elements in a vector by three threads, I am confused why its behavior is not synchronized .



Output is:
Thread-3
Thread-5
Thread-1
Thread-5
Thread-1
Thread-3
Thread-5
Thread-3
Thread-1
Thread-5
Thread-3
Thread-1
Thread-5
Thread-3
Thread-1
15
element0
element0
element0
element1
element1
element1
element2
element2
element2
element3
element3
element3
element4
element4
element4

Here if vector's add() method is synchronized then result should be
element 0
element 1
element 2 ... and so on

I am confused ???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Not at all. Synchronisation means that only one thread can add something at any one time. It does not mean that the threads finish before the next can start.
What is happening is that one thread adds element0, then sleeps. While it is sleeping, the add method is unlocked, and another thread can use it to add element0 again.

At least that is what I think is happening. I hope you don’t use legacy code like Vector in real life. And shouldn’t it be a Vector<Integer>?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
Here if vector's add() method is synchronized then result should be...

Campbell's explanation is correct but just to add some extra info you would get the result you desire if you synchronize the share method because then you would be preventing another thread from accessing the share method and hence the vector.add() method until the current thread has added its 5 elements.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:Not at all. Synchronisation means that only one thread can add something at any one time. It does not mean that the threads finish before the next can start.
What is happening is that one thread adds element0, then sleeps. While it is sleeping, the add method is unlocked, and another thread can use it to add element0 again.

At least that is what I think is happening. I hope you don’t use legacy code like Vector in real life. And shouldn’t it be a Vector<Integer>?


Just to clarify a bit...

The sleep() call has nothing to do with it, except perhaps giving the scheduler a reason to give another thread CPU time when otherwise a given thread might be able to accomplish all its work in a single time slice, which would make it appear as if things were behaving the way you initially expected them to. And calling sleep() does not release any locks.

The add() method is synchronized, but that only matters while a thread is executing it. As soon as a thread leaves the add method, the lock is released and, another thread can obtain the Vector's lock and enter the method, even if the first thread is in a loop and is going right back around to add() again. If you're on a multi-CPU or multi-core system, there's a good chance that there will be some thread alternation even without the sleep.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
The sleep() call has nothing to do with it ... And calling sleep() does not release any locks.

Absolutely agree, but I don't think Campbell was saying that sleep did release any locks. I read it as "during the time the thread is sleeping the vector's monitor is not locked (because you are no longer in the vectors add method)" and not as "once the thread goes to sleep the vector's monitor is released".
Either way your post clears up any potential misunderstandings.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tony Docherty wrote:
The sleep() call has nothing to do with it ... And calling sleep() does not release any locks.

Absolutely agree, but I don't think Campbell was saying that sleep did release any locks. I read it as "during the time the thread is sleeping the vector's monitor is not locked (because you are no longer in the vectors add method)" and not as "once the thread goes to sleep the vector's monitor is released".
Either way your post clears up any potential misunderstandings.


Yeah, I figured that's what he meant, but I could see how the OP might misinterpret it and read more into the role of sleep() than there really is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector Synchronization