Sike singleton design pattern, starving, all get lazy

Singleton

Singleton pattern may be minimal code mode, but does not necessarily mean less simple, you want to make good use singleton mode, really have to cost a lot of brains. In this paper, common in Java Singleton made a written summary, if errors and omissions, urge readers to correct me

Hungry man

1. hungry man if the application is always to create and use a singleton instance or little overhead in creating and running

classSingle {private Single(){} private static Single single= newSingle(); public static Single getInstance(){ return single; } 

Hungry man single example in the class loader created a static initialized object for external use, unless the system is rebooted, the object does not change, it is itself thread-safe.

Singleton defined by the constructor of the class is instantiated avoids externally, a virtual machine within the same range, the only instance can only be accessed Singleton getInstance () method is private. (In fact, through the Java reflection mechanism is able to instantiate the class constructor is private, and that basically makes all the Java failure to achieve a single case. This issue is not discussed here, tentatively eyes closed not considered reflection presence.)

Sluggard

2. Lazy If the overhead is relatively large, want to use when you create instances of the Singleton should consider delaying the initialization requires some external resources (such as network or storage device), following such an approach is thread-safe, because at the same time when a number getInstance threads in the implementation of, if 2 if not, it is possible to find in the first place are null if 1, when in a synchronized block of code is completed, it is possible to have a plurality of synchronized threads are waiting outside, this time synchronized block of code other end, or will walk new single (); therefore, this case is not a single case, and if so at @ 2 must have.

class Single {private Single(){} private static Single single= null; public static Single getInstance(){ if ( single == null ) { //1 synchronized (Single.class) { if ( single == null) { single = newSingle(); } } } return single; } } 

Such an approach can work well in multi-thread, but need to be synchronized every time getInstance method is called, causing unnecessary synchronization overhead, and most of the time we are less than synchronized, so I do not recommend this mode

Static inner classes

classSingle { privateSingle(){} privatestatic class SingleHandler {private static Single single = new Single(); } public static Single getInstance(){ return Single. SingleHandler.single; } 

This creates an internal static class by class such that the internal mechanism can delay singleton object loading, while the inner portion of the outer static class corresponding to the class, it is possible to ensure safety by its thread JVM. This form is more recommended.

enumerate

publicclassSingle { private Single(){} public enum SingleEnum { singleHandler; private Single single; privateSingleEnum () { single = new Single(); } public Single getSingle() { return single; } } public static Single getInstacne() { return SingleEnum.singleHandler.getSingle(); } } 

Enumeration method to achieve a single case of the most simple, this is because the creation itself Enum class is thread-safe, and it is similar to the static inner classes, so we do not have to care what DCL (double checked locking) problem. In fact, the idea is to enumerate through shared and exported static final instance for each enumeration constant class, because the constructor is not accessible, so you can not call the constructor method of enumeration constants to generate a corresponding object, so the "Effective java ", the enumerated type is type-safe enumeration mode, also called the enumeration of generic single embodiment.

Examples of single-mode application scenario

Previous analysis of the structure and features of the single-mode embodiment, the following characteristics are generally suitable for its scene.

  • In the scenario, some sort requires only generated when an object is, as a class in the squad, everyone's ID number and so on.
  • When the object needs to be shared occasions. Since the Singleton pattern allows the creation of an object, the object can be shared to save memory and speed up the access speed of the object. The configuration of the Web objects, database connection pooling, and the like.
  • When certain types of needs frequent instantiated objects created and destroyed frequently, such as multi-threaded thread pool, Internet connection pools.

I welcome everyone's attention, and leave a valuable footprint, have any questions can post discussion ~

Guess you like

Origin blog.csdn.net/Java__xiaoze/article/details/94743987