The reason and solution of single example acquisition and serialization and deserialization failure

Source: Internet
Author: User
Tags serialization static class

The singleton pattern is the entire global (either single-threaded or multi-threaded), with only one instance of the object, and there should be only one instance, with no replicas (the production of replicas takes time and space resources). In its core structure, there is only one special class called a single example. A single example mode can guarantee that a class in a system has only one instance and that the instance is easy to access outside, thus it is convenient to control the number of instances and save the system resources. The single case pattern is the best solution if you want the object of a class in the system to exist only one, and the object needs to coordinate the behavior of the system as a whole. The single example pattern is equivalent to a system with only one portal, which makes it possible for all objects that want to acquire the system's resources to pass through the portal.

The way and the advantages and disadvantages of single example realization

/** * Created by Chenay on 2016/4/5.
    * * Public class Singleton implements serializable{private static Singleton Singleton; transient int i; Short/** * Single case mode 1 * Advantage: simplest; is called lazy because it delays the initialization of a single case to the first call * Disadvantage: thread unsafe * *@return */public static Singleton getinstance () {if (Singleton = null) {Singleton = new Singleton ();
    return singleton; /** * Advantages: Thread safety * Disadvantage: 99% sync is much with the * *@paramS *@return */public static synchronized Singleton getinstance (String s) {if (Singleton = = null) {Singleto
        n = new Singleton ();
    return singleton; /** * Advantages: Single case only, thread safety * disadvantage: JDK1.6 above; slow to load first; occasionally fail (double check lockout failure) *@paramI@return * * private static Object Synobj = new Object (); public static Singleton getinstance (int i) {if (instance = null) {///First judge if no instantiation synchronized (synobj) {///If Request lock if (instance = null) {//Get lock is not instantiated, again to determine if it has been instantiated by the previously acquired lock object instance =
                New Singleton ();
    }} return instance;
     /** * Benefits: Code simplicity, no need for synchronous lock * Disadvantage: The reason is called a hungry man, because as long as the compiler sees the class will initialize the single example, can not achieve the purpose of * delay loading.

    * private static Singleton instance = new Singleton ();
    Private Singleton () {} public static Singleton getinstance (int i, int synobj) {return instance; /** * Benefits: Thread safety, object uniqueness, Deferred instantiation * Disadvantage: no * * private static class Singletonholder {private STA
    Tic final Singleton Singleton = new Singleton ();
    public static Singleton Getinstence () {return singletonholder.singleton; //Add this hook function, then the system in the deserialization process will be through the hook method to get the original single case//withoutis to recreate a single instance.
    Private Object Readresolve () throws objectstreamexception {return Singletonholder.singleton; }
The destruction of a single case by serialization

First, write a single instance of the class:

Code 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20
public class Singleton implements serializable{private volatile

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.