Android Classic single-case mode

Source: Internet
Author: User

When the Singleton class is loaded for the first time, Sinstance is not initialized, only when the GetInstance method of Singleton is called for the first time, which causes sinstance to be initialized. Therefore, calling the GetInstance method for the first time causes

The virtual machine loads the Singletonholder class, which not only ensures thread safety, but also ensures uniqueness of singleton objects and also delays the instantiation of Singleton, so this is the recommended singleton mode

public class Singleton {     private Singleton () {};       public static Singleton getinstance () {         return singletonholder.sinstance;     }     /**      * Static inner class */     private static class singletonholder{         private static final Singleton sinstance = new Singl Eton ();     }}

This method may seem like a good one, but there seems to be a double check lock (DCL) failure.

public class Myimageloader extends Imageloader {    private static myimageloader instance;        public static Myimageloader getinstance () {        if (instance = = null) {            synchronized (myimageloader.class) {                if ( instance = = null) {                    instance = new Myimageloader ();}}        }        return instance;    }        Protected Myimageloader () {            }}

Android Classic single-case 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.