Single-Case Mode learning

Source: Internet
Author: User

Reference website:

1.Java Multithreaded Learning notes

2. Simple and thread-safe two single-mode Java programs

3.Implementation of the singleton pattern in the Java Multithreaded Programming environment (inner classes implement singleton patterns in multithreaded environments)


Main content:

What is a singleton mode? The singleton pattern is a design pattern, and the idea is that a special class, which has only one instance, provides a way to access the class;


The singleton pattern can be divided into a hungry man type and lazy type from the realization:

A Hungry man type:

The singleton mode ———— the code specification for "A Hungry Man" class singlehungry{//Here is the keyword final modifier asingleinstanceobjref, which indicates that the secondary object is not modifiable,//Although not final can achieve the effect, But this is more normative. private static final Singlehungry Asingleinstanceobjref = new single (); Private Singlehungry () {} public singlehungry Getsingle () {return asingleinstanceobjref;}}

Advantages: Thread safety;

Disadvantage: It has been instantiated before it was called, consuming the corresponding system resources.


Lazy Type:

The singleton mode ———— "lazy" code//The following code is "thread unsafe" notation. Class singlelanhan{//cannot have a keyword final here, otherwise the Asingleinstanceobjref reference cannot be copied,//because it is determined to be a constant null private static Singlelanhan Asingleinstanceobjref = null; Private Singlelanhan () {} public Singlelanhan Getsinglelanhan () {if (asingleinstanceobjref = = null) {//A Asingleinsta  Nceobjref = new Singlelanhan ();   B} return asingleinstanceobjref; }}

Advantage: It is instantiated only at the time of invocation;

Disadvantage: Thread is not secure. Cause: Thread 1 and thread entered a at the same time, thread 1 preempted the resource, executed B, then thread B also obtained the resource, executed B, when the class was instantiated two times, resulting in thread insecurity.


Solution One (locking):

The singleton mode ———— "lazy" code//The following code is "thread unsafe" notation. Class singlelanhan{//cannot have a keyword final here, otherwise the Asingleinstanceobjref reference cannot be copied,//because it is determined to be a constant null private static Singlelanhan Asingleinstanceobjref = null;   Private Singlelanhan () {} public synchronized Singlelanhan Getsinglelanhan () {if (asingleinstanceobjref = = null) {//A  Asingleinstanceobjref = new Singlelanhan ();   } return asingleinstanceobjref; }}

This solution uses the Synchronized keyword pair to prevent different threads from entering a at the same time, because the singleton mode is instantiated only once, in the subsequent acquisition of the instance, it is necessary to determine whether there are other threads occupying the lock, inefficient.


Solution two (double lock):

//singleton mode ———— "lazy" code specification class singlelanhan{ //there is no keyword final here, Otherwise the Asingleinstanceobjref reference cannot be copied,  //because it is determined to be a constant null private static singlelanhan  Asingleinstanceobjref = null; private singlelanhan ()  {} public singlelanhan  getsinglelanhan () {      if (asingleinstanceobjref == null)  {      //a   synchronized (Singlelanhan.class)  {         if (asingleinstanceobjref == null)  {      Asingleinstanceobjref = new singlelanhan ();     return  asingleinstanceobjref;    }   }  }else {    return asingleinstanceobjref;  } }} 

This solution adds an if statement (a) before the Synchronized keyword, and the synchronized lock still works before it is instantiated, and a judgment statement is required inside the synchronized. Prevents threads from being repeatedly instantiated after they have entered the synchronized block, and after instantiation, it (a) functions to isolate the operation of the synchronized block. The efficiency ratio of the programme is high.


Solution three (inner class):

public class Singleton {static class Singletonholder {static Singleton instance = new Singleton ();} public static Singleton getinstance () {return singletonholder.instance;}}

The mechanism inside the JVM guarantees that when a class is loaded, the loading process of the class is thread-exclusive. So when we first call getinstance, the JVM can help us ensure that instance is created only once and that the memory assigned to instance is initialized, and Instance is created the first time the Singletoncontainer class is loaded, and the Singletoncontainer class is loaded when the GetInstance method is called, so lazy loading is also implemented.


The small single-case pattern is still such a big learning, first recorded, for review only. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0028.gif "alt=" J_0028.gif "/>

This article is from the "Noobtidehunter" blog, make sure to keep this source http://noobtidehunter.blog.51cto.com/10271860/1723442

Single-Case Mode learning

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.