• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Static variable handled during concurrent Syncronized Class and Object Call  RSS feed

 
Gregory Garrison
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please tell me what happens when a static variable is being accessed (or an attempt to access) by an instance method and both the Class and Object are called using Syncronized methods (static and instance).
Thanks
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite sure what you're after. Inthe thread must acquire a monitor lock on the AccessTest instance before it can execute the method, regardless of whether <code>variable</code> is an instance variable or a static variable. On the other hand, inthe thread must acquire a monitor lock on the class object, i.e. <code>AccessTest.class</code>.
Whether this gives the right locking semantics when accessing <code>variable</code> depends on (a) how the other code accessing this variable is synchronized and (b) if the synchronization is not locking out needlessly many other threads. For instance, if the variable is an instance variable then you generally want to synchronize on the instance rather than the class object (which is shared by all instances).
Does that help?
- Peter
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!