Java singleton design pattern

Source: Internet
Author: User

Public class Singleton {

Private static Singleton instance = null; private Singleton () {} private static synchronized void syncInit () {if (instance = null) {instance = new Singleton ();}} public static Singleton getInstance () {if (instance = null) {syncInit ();} return instance;} application: Generally speaking: The Singleton mode is generally in, you only want to use one thing and cannot use it when adding it. For example, in QQ chat, you only need to open a dialog box, no matter how many times the friend is clicked or the first window, to control resource usage, use thread synchronization to control concurrent resource access. Second, control the number of instances generated to save resources. Third, it is used as a communication media, that is, data sharing. It can enable communication between multiple unrelated two threads or processes without establishing a direct association. For example, the database connection pool is generally designed in the singleton mode. database connection is a type of database resource.

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.