• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Vector Synchronization

 
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 need a new interior decorator. This tiny ad just painted every room in my house purple.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic