• 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

synchronized or notsynchronized

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



In the above code, do i need to synchronize the method "loadProperties" or not. Would it create some error if synchronization is not applied here?
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, that it would not create an error, but in worst case you'll end up reading properties more than once.

But synchronizing whole method may slow your program down if it is frequently called and also if you have multiple instances of that object, synchronizing on 'this' will not be effective.

I would go for synchronized block on propObject with extra check before it if propObject!=null. This would eliminate all synchronization on subsequent calls, but I can be sure, that Properties will be created only once.

Also you can change your 'lazy' initialization to non-lazy and create it immeadiatly when class is loaded.
 
gurpreet singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what difference would it be if i do :
synchronization on class objet



or synchronization on propObject

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gurpreetgh Singh:
In the above code, do i need to synchronize the method "loadProperties" or not.

You don't need synchronization, since the method does always nothing.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gurpreetgh Singh:


It's impossible to use a null value for synchronization.
 
gurpreet singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is the above implementation insures that the property file is loaded by only one thread?
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic