Java Virtal Machine.net

[ News ] rss

February 01, 2011

Mark Wielaard: New GPG key. Finally created a new GPG key using gnupg. The old one was a DSA/1024 bits one and 8 years old. The new one is a RSA/2048 bits one. I will use the new one in the future to sign any release tarballs I might create. pub 2048R/57816A6A 2011-01-29 Key f...

More »

February 01, 2011

Andrew Hughes: [SECURITY] IcedTea6 1.7.8, 1.8.5, 1.9.5 Released!. We are pleased to announce a new set of security releases, IcedTea6 1.7.8, IcedTea6 1.8.5 and IcedTea6 1.9.5. This update contains the following security updates: The IcedTea project provides a harness to build the source code from OpenJDK6 u...

More »

March/2024
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
242526272829 30
31      

[ Archives News
for 'Java Technology' ]

home > news > java technology > jsr 292 goodness: lazy singleton pattern

JSR 292 Goodness: lazy Singleton Pattern

The idea of the lazy Singleton Pattern is to initialize a static field only when needed and not during the initialization of the class. The code for that pattern is this one: private static Database database; public static Database getDatabase() { synchronized (DCL.class) { if (database == null) { database = new Database(); } return database; } } In that case, you pay the synchronization cost each time you access to the variable. Note that it's far from obvious that in a real application this synchronization cost is significant, but for the rest of this blog, let say that this cost is not acceptable. As you probably already know the Double-Checked Locking idiom (DCL) is broken. The recommanded idiom is to declare another class and to use natural lazy loading of the class in Java.  static class Lazy { private static final Database database = new Database(); } public static Database getDatabase() { return Lazy.database; } In this blog post, I want to show you another way to solve that problem using invokedynamic. The invokedynamic instruction has a powerful initialization mechanism, the first time an invokedynamic instruction is seen a bootstrap method (a factory method) associated to the invokedynamic instruction is called to get a method handle (a safe function pointer) that will be called for the current call and the next calls. public static Data...


Date: November, 05 2010
Url: http://www.java.net/blog/forax/archive/2010/11/04/jsr-292-goodness-singleton-pattern


Others News

©2002-2019Java-Virtual-Machine.net website designing software | Mobirise Templates | Bootstrap Templates | top website builder | bootstrap modal popup