Rookie growth Diary: Java Fundamentals 3---singleton mode

Source: Internet
Author: User

The singleton pattern is divided into lazy and a hungry man types, the following is the comparison between the two:

1, a hungry man load immediately, when the class is loaded, an object is created for external use.

Lazy lazily loading, which creates an object only when it is called.

2, according to the above characteristics can know:

A hungry man is thread-safe, lazy-type causes access to objects is not thread-safe, you should pay attention to the use of

Synchronized keyword.

3, both have the advantages of each have a problem, lazy-type delay loading to improve efficiency, a hungry man-type thread safety.

The business tradeoff between efficiency and security

Here is an example of a cache instance that encounters an instance of the job:

The first lazy-man

1 /**2 * Players open the Carnival package3 * @author Wangchong4 * @date August 25, 2017 morning 10:51:315  */6  Public classPlayerinitialactivitycache extends Playertimeidlecache<playerinitialactivity> {7 8     Private StaticFinal Playerinitialactivitycache instance =NewPlayerinitialactivitycache ();9 Ten     PrivatePlayerinitialactivitycache () { OneSupertrue, Datetimeutil.minute *Ten); A     } -  -      Public StaticPlayerinitialactivitycache getinstance () { the         returninstance; -     } -}

The following a hungry man is just a demonstration of the need to temporarily change the actual use is not recommended

1 /**2 * Players open the Carnival package3 * @author Wangchong4 * @date August 25, 2017 morning 10:51:315  */6  Public classPlayerinitialactivitycache extends Playertimeidlecache<playerinitialactivity> {7 8     Private StaticPlayerinitialactivitycache instance =NULL;9 Ten     PrivatePlayerinitialactivitycache () { OneSupertrue, Datetimeutil.minute *Ten); A     } -  -      Public StaticPlayerinitialactivitycache getinstance () { the         if(Instance = =NULL) { -Instance =NewPlayerinitialactivitycache (); -         } -         returninstance; +     } -}

Rookie growth Diary: Java Fundamentals 3---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.