Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
Beaker
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic