Java Singleton mode

Source: Internet
Author: User

There are only two types of singleton patterns: lazy and a hungry man, and then, because you see a word, if you privatize the constructor, others use reflection, this time what to do, so you know the third way to enumerate patterns. And then today you see the static internal class and double detection lock, today to summarize.

One, single case mode

(1) Core: Ensure that there is only one instance and provide an access point for accessing the global.

(2) Use the scene:

Task Manager for Windows

. Recycle Bin for Windows

Project, it is not necessary to read the configuration file every time.

Website counter

Log Management

Database connection pooling, and so on

(3) Advantages

Using a singleton to generate only one object, reducing the overhead of the system, and generating more resources for an object, such as configuring, generating dependent objects, we can use singleton objects

Singleton mode can set global access points and share access to resources.

2. How to implement a single case design pattern

(1) A Hungry man: thread-safe, high-efficiency calls, but no delay loading

A) First private constructor

b) Create a method with a private static variable

c) provides a static way to access the secondary object

It is a thread-safe pattern that objects are not created by themselves, but have an object waiting to be used, without synchronization, and with high efficiency.

Wasted resources, no lazy loading.

Implementation method:

Package KW.TEST.SJMS; /* * Singleton mode *     a hungry man singleton mode  **/publicclass  demoeh {    private  staticnew  Demoeh ();     Private Demoeh () {}      Public  Static Demoeh getinstace ()    {        return  instance;    }}

(2) Lazy: thread-safe, call efficiency is not high, but can delay loading

··· load only when used, but need to use synchronized

· ··· Resource utilization is high, but efficiency is not very high.

··· Cons: Every time you need to sync, waste resources.

Implementation method:

Package KW.TEST.SJMS;/** Lazy Single-case mode*/ Public classDemolh {Private StaticDEMOLH instance; PrivateDemolh () {} PublicSynchronizedStaticDemolh getinstance () {if(Instance = =NULL) {instance=NewDemolh (); }        returninstance; }}

(3) double detection Lock: Due to the internal model of the JVM layer, occasionally there will be problems, not recommended.

• Solve the lazy-type of every time you need to lock judgment of the resource waste.

• The lock is placed inside the If judgment, which improves efficiency and does not need to be synchronized every time.

Package KW.TEST.SJMS;/** Dual-lock single-case mode*/ Public classDemoscs {PrivateDemoscs () {}Private StaticDemoscs instance =NULL ;  Public StaticDemoscs getinstance () {if(Instance = =NULL) {Demoscs Demoscs; Synchronized (Demoscs.class) {                if(Instance = =NULL) {instance=NewDemoscs (); }                        }        }        returninstance; }}

(4) Static internal class: Thread-safe, high-efficiency call, but can delay loading

• Thread-safe, more efficient than lazy, and lazy-loaded.

Package KW.TEST.SJMS;/** Static inner class*/ Public classDEMOJTNB {Private Static classCreateInstance {Private StaticFinal DEMOJTNB instance =NewDEMOJTNB (); }    PrivateDEMOJTNB () {} Public  StaticDEMOJTNB getinstance () {returncreateinstance.instance; }}

(5) Enumeration singleton: thread safety, high call efficiency, cannot delay loading, can prevent reflection from being created.

• Enumeration is a natural singleton. However, there is no delay in loading the functionality.

Package KW.TEST.SJMS;  Public enum DEMOMJ {    instace;      Public void getinstance () {    }}

The best pattern: thread safety, high call efficiency, lazy loading. The most perfect way to achieve.

Java Singleton mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.