Design patterns: How to Style a singleton (basic notation and thread-safe notation)

Source: Internet
Author: User
Tags volatile

A singleton pattern is very much written. First, the most basic way to write:

(a type of notation):

Package Singleton;public class Singletoninstance {private static singletoninstance msingletoninstance = null;// Deliberately set the constructor to private to prevent external user new singletoninstance (). Private Singletoninstance () {}public static singletoninstance getinstance () {if (msingletoninstance = = null) { Msingletoninstance = new Singletoninstance ();} return msingletoninstance;}}

A notation is the simplest, most basic, and clearest form of a singleton pattern. Unfortunately, this is a thread-unsafe code notation.

It would be a disaster to assume that the class is being used at the same time in concurrent n multiple threads, especially if this class of assumptions involves database access, and so on, sensitive code scenarios such as thread-safety issues.

But the one-way approach to the singleton pattern also has a wide range of scenarios: no thread-safe, no synchronization requirements, and high-efficiency priorities. It is recommended to use a one-way notation for singleton patterns.


b Notation (thread-safe notation 1):


Package Singleton;public class Singletoninstance {private static singletoninstance msingletoninstance = null;// Deliberately set the constructor to private. Prevent external user New singletoninstance ().

Private Singletoninstance () {}public static synchronized singletoninstance getinstance () {if (msingletoninstance = = NULL {msingletoninstance = new singletoninstance ();} return msingletoninstance;}}


The B-style of the singleton pattern is actually an improvement on the basis of a type of writing. The point is to add a synchronization mechanism: synchronized.

Synchronized, synchronization is, in a sense, a blockage, the result of which is a random moment, and only one thread can access the code in the body of the synchronization method. This will degrade the performance of the code being synchronized, but it achieves thread-safe purposes.

The formulation of the B-type single-case pattern. Primarily to address thread safety.

There are many variations. The purpose of the variant is mainly focused on how to enhance the operability of thread safety. The present ratio is as follows, for example:

Enhanced variants of B notation (thread-safe notation 2):

Package Singleton;public class Singletoninstance {//Note!

Volatile also does not guarantee thread safety at all, and later writes about volatile articles explaining this. Volatile is just an enhancement.

private static volatile singletoninstance msingletoninstance = null;//deliberately sets the constructor to private. Prevent external user New singletoninstance (). Private Singletoninstance () {}public static singletoninstance getinstance () {if (msingletoninstance = = null) {//Synchron Ized (Singletoninstance.class) does not have to lock the entire method, just lock a piece of code. Same as synchronized (this), but because it is a static method. This cannot be used. Use xxxclass.class//synchronized basic use principle: as far as possible do not lock large chunks of code (method body or Class), just lock the necessary small block of core, need to synchronize the code snippet, the less the better, the smaller the better. You know, once you use synchronized, it means the cost of code performance synchronized (Singletoninstance.class) {if (msingletoninstance = = null) Msingletoninstance = new Singletoninstance ();}} return msingletoninstance;}}




Design mode: How to Style a singleton pattern (basic writing and thread-safe notation)

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.