Java Singleton mode

Source: Internet
Author: User

The singleton mode has the following characteristics:
  
1, the Singleton class can have only one instance.
  2, the Singleton class must create its own unique instance.

  3. The Singleton class must provide this instance to all other objects

Way one, lazy single case, thread unsafe

public class Singleone {//lazy-slip singleton, thread insecure private static singleone single = Null;private Singleone () {}// Set private to prevent foreign key new an object public static Singleone getinstance () {if (single = null) {System.out.println ("new");  new Singleone ();} return single;}}
Mode two,//a Hungry man type single case, thread safety
public class Singletwo {//A hungry man type singleton, thread safe private static final Singletwo single = new Singletwo ();p rivate singletwo () {}public St atic singletwo getinstance () {System.out.println ("new single"), Return single;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Singleton 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.