• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization  RSS feed

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone recently asked me in an interview this about the Vector class.

Is it synchronized at the object level or the class level?

I don't even understand what the above means. Could someone please explain? I know what synchronization is and how we synchronize in Java using the synchronized keyword or the synchronized block. But I can't relate those to the question.
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's synchronized at the object level. That is, any methods that alter the state of a single Vector instance are synchronized so that only one thread can be operating in one at a time. However, two threads could simultaneously alter the state of two different Vector objects. For something to be synchronized at the class level means that static methods are synchronized, or that a block is synchronized using the class monitor, rather than an instance's object monitor. Synchronization of that type would mean that a thread wanting to enter a synchronized block would have to wait until other threads operating on synchronize blocks of any instance of the class were done.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Greg. Is there an example of class level synchronization in Java? And, how do you do synchronization using the class monitor?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is object synchronisation and what is class synchronisation? Are the two different?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was more discussion about synchronisation yesterday: try this thread.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Someone recently asked me in an interview this about the Vector class.

Is it synchronized at the object level or the class level?


There isn't really any such thing as synchronizing at the "object level" or "class level". There are no levels. All synchronization in Java works exactly the same way: We obtain an object's lock, and no other thread can obtain that lock until we release it, either by exiting the sync block, or by calling wait(), or by the current thread terminating.

I assume what he meant is the way Greg describes it in the first answer--syncing all the methods on their this object as opposed to syncing them on the Vector.class object. I think it's misleading to call that "object level" or "class level" though.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!