• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double-Checked Locking or DCL

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

Does using the Double-Checked Locking or DCL idiom make any sense especially with multi core and advancement in parallelism around these days?

Thanks
Sathya
 
Saloon Keeper
Posts: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double checked locking doesn't make sense, but not for the reasons you mentioned.

First off, it's simply bad design. Objects shouldn't be expensive to construct, and even if they are, there's probably no reason to keep them around in a static field.

Secondly, even IF you find some reason to lazily load an expensive object and make it globally accessible from a multi-threaded environment, there's a much easier idiom to use than double checked locking: The initialization-on-demand holder.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
First off, it's simply bad design. Objects shouldn't be expensive to construct, and even if they are, there's probably no reason to keep them around in a static field.



I think the original argument was that synchronization was expensive. And avoiding it completely after the object has been constructed was a benefit.

The problem with the argument is many fold. First, it's not a reasonable use case. Yes, if you call the method a billion times, you may notice that the unsynchronized version may be faster... but is anyone going to seriously make a billion calls to a get a singleton in a back to back fashion? Second, synchronization has improved over the years. And synchronization of an uncontended lock has dramatically been improved... making it even less noticeable.

Henry
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic