• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

synchronized block

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a class similar to the one below, and have a synchronized block for the object level lock on "_object", will that prevent the getStaticObject() method from returning the "_object", which may be null, if the reinitObject() method is called?


It seems like both methods should be static and synchronized, and the synchronized block should synch on the class level lock... to prevent the getStaticObject() method from returning a null, right?


Which is the best approach to preventing getStaticObject() from ever returning null?

Thanks,
Becker
[ July 07, 2005: Message edited by: Beaker ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Beaker A. Muppet": Welcome to JavaRanch! Please take a look at our display name policy and edit your display name to something less obviously farcical. Thank you.

Also, most of us find it difficult to read posts in which the code has no formatting. Please use [code] tags to get the formatting to display so it's readable. You can edit your previous post using the little pencil-and-paper icon at the top of the post.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is...

To safeguard a static variable, you need to synchronize on the class rather than a particular object. Otherwise, different threads could obtain object locks of different instances and access the (same) static member in an "unsynchronized" manner.

Because your methods are already static, you only need the "synchronized" keyword before the return type -- you do not need a separate block synchronized explicitly on the class.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are things I could say, but you would learn enough already buy creating code that compiles first. Start there.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic