`
zhoulf
  • 浏览: 4876 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Singletons and lazy loading(转载onjava)

    博客分类:
  • java
阅读更多

Probably the first design pattern that every software developer learns is Singleton and lazy loading of Singleton classes.

The usual example, goes something like this:

public class Singleton {

 static Singleton instance;

  public static synchronized Singleton getInstance() {
    if (instance == null)
      instance = new Singleton();
    return instance;
  }

}

The problem with this solution is that synchronized method getInstance() is called every time, while synchronization is actually needed only for the first call of the method (which introduces performance overhead in your application). There were attempts to tackle this problem by using Double-checked locking pattern, which although great in theory didn’t work in practice.

Today, thanks to Bob Lee, I found out that there is a solution to this problem that is both simple and fast: Initialization on Demand Holder (IODH) idiom. The appropriate example follows:

public class Singleton {

  static class SingletonHolder {
    static Singleton instance = new Singleton();
  }

  public static Singleton getInstance() {
    return SingletonHolder.instance;
  }

}

Basicaly, Java Language Specification (JLS) guarantees that instance would not be initialized until someone calls getInstance() method (more information could be found in articles that I’ve linked to before). Elegant and fast, just as it should be.

分享到:
评论

相关推荐

    Lazy Loading Singletons

    在Java中,一种常见的懒加载单例实现方式是双重检查锁定(Double-Checked Locking,DCL)机制。以下是一个简单的DCL实现示例: ```java public class Singleton { private volatile static Singleton instance; ...

    PyPI 官网下载 | singletons-0.2.1.tar.gz

    标题 "PyPI 官网下载 | singletons-0.2.1.tar.gz" 提到的是一个从 Python Package Index (PyPI) 官方网站下载的软件包,名为 "singletons-0.2.1.tar.gz"。这个压缩包是Python编程语言的一个库,版本为0.2.1。在...

    effective-java 配套代码

    《Effective Java》是Java开发领域的一本经典著作,由Joshua Bloch撰写,书中提出了一系列编程最佳实践和设计模式,帮助开发者写出更高效、更可靠、更易于维护的Java代码。配套代码`effective-java-examples-master`...

    Game Development Projects with Godot 3-September 21, 2019.z02

    Design elegant systems for use with Singleton nodes, allowing you to efficiently handle resource loading and background music Connect and elegantly handle signals between nodes, allowing you to easily...

    singletons:Haskell中使用单例的虚假依赖类型

    单身人士 这是singletons singletons-th库, singletons-base库和singletons-base库的自述文件。 该文件包含这些库中的定义和功能的文档。 singletons库由Richard Eisenberg( )编写,Jan Stolarek( )和Ryan ...

    [视频教程][Eng]Game Development Projects with Godot 3-September 21, 2019.z01

    Design elegant systems for use with Singleton nodes, allowing you to efficiently handle resource loading and background music Connect and elegantly handle signals between nodes, allowing you to easily...

    Game Development Projects with Godot 3-September 21, 2019.zip

    Design elegant systems for use with Singleton nodes, allowing you to efficiently handle resource loading and background music Connect and elegantly handle signals between nodes, allowing you to easily...

    如何使Singletons的行为可以被Mock对象覆盖

    在Java开发中,Singleton模式是一种常用的创建型设计模式,它保证了类只有一个实例,并提供一个全局访问点。然而,Singletons在单元测试中往往成为挑战,因为它们的静态实例化和不可变性使得它们难以被Mock或Stub。...

    Struts2+Spring3+MyBatis3完整实例

    - The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: E:\Java\jre1.5.0_17\bin;.; - Initializing Coyote HTTP/1.1 on ...

    EFFECTIVE_JAVA_3RD

    10. **Lambda表达式和函数式接口(Lambdas and Functional Interfaces)**:Java 8引入的新特性,讲解了如何使用lambda简化代码,以及函数式接口如`Runnable`、`Supplier`和`Consumer`的作用。 11. **流(Streams)...

    Java集合排序及java集合类详解[收集].pdf

    Java集合框架是Java编程语言中的核心部分,它提供了一种高效、灵活的数据组织方式,使得开发者可以方便地存储和操作各种类型的数据。本文将深入解析Java集合框架中的主要类和接口,包括Collection、List、Set和Map,...

    Lilith:Type用TypeScript制作的简单应用程序框架(供个人使用)

    @ augu / lilith :thread: 使用TypeScript制作的简单应用程序框架(供个人使用)用法import { Application } from '@augu/lilith' ;... // Verify all components, singletons, and services and implements them// =>

    unity迷你太空射击游戏源码

    游戏截图: ...* Work with global data through Persistent Singletons * Use ob ject pooling to prevent performance loss. and the basics of 2D games: * Sprite movements * Collisions * Play sounds

    type-combinators-singletons:类型组合器和单例库之间的互操作

    类型组合单 数据类型与单和孤立实例中的单之间的转换。 两个库之间在功能上有很多重叠。 我经常同时使用它们来做不同的事情,但是在两个库具有的相同数据类型之间以及相似的类型类之间进行转换的过程有些麻烦。...

    testSpring

    信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e234c: defining beans [personService]; root of factory hierarchy save()方法调用

    『手撕』单例模式

    2. 懒汉方式(Lazy Way) 懒汉模式在第一次调用 `getInstance` 方法时才创建实例,实现了延迟加载。但原始的懒汉模式(如上述代码所示)在多线程环境下不是线程安全的,可能导致多个线程同时创建实例。 3. 双重...

    EJB系统开发实战录.pdf

    现代Java EE倡导更简洁的编程模型,如使用CDI(Contexts and Dependency Injection)和Quarkus等框架。 总结,"EJB系统开发实战录.pdf"这本书旨在帮助读者掌握EJB技术,并通过实际的在线Seminar注册系统案例,将...

Global site tag (gtag.js) - Google Analytics