


This article mainly introduces the relevant information of java singleton mode. Here are examples of lazy man style and hungry man style. Friends who need it can refer to it
java singleton mode
The singleton pattern is a commonly used software design pattern. A special class that contains only one instantiated singleton in its trusted structure. Through the singleton design pattern, a class in the entire system can have only one instance.
The singleton design pattern is divided into two methods, the lazy man style and the hungry man style.
(1) Lazy style, that is, this singleton will be initialized only when getInstance is called.
(2) Hungry Chinese style, that is, once the class is loaded, the singleton initialization is completed. That is, when getInstance is called, the singleton already exists.
The code is as follows:
Lazy Chinese style singleton
/** * Description: * 一、设计了一个懒汉式单例 * * @author lee * */ public class SingleMode { public static SingleMode s = null; /** * Description: * 私有化构造函数 * * */ private SingleMode(){} /** * Description: * 提供一个方法来获取该单例 * * @return s 返回一个单例 * */ public static SingleMode getInstance(){ //保证了线程安全,又减轻了资源加载和性能上的负担 //即是在创建一次对象后,不再需要线程锁了。 if(s==null){ synchronized(""){ if(s==null){ s = new SingleMode(); } } } return s; } }
Hungry Chinese style Singleton
##
/** * Description: * 二、设计了一个饿汉式单例 * * @author lee * */ class SingleMode1{ public static final SingleMode1 s = new SingleMode1(); /** * Description: * 私有化构造函数 * * */ private SingleMode1(){} /** * Description: * 提供一个方法来获取该单例 * * @return s 返回一个单例 * */ public static SingleMode1 getInstance(){ return s; } }
The above is the detailed content of Detailed analysis of singleton pattern in Java about lazy style and hungry style. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.