Synchronized usage in singleton

Source: Internet
Author: User
I have been looking at some things in JAVA recently. I have some experiences in the use of object-oriented languages such as C, PHP, and JAVA over the years. There is no good or bad language. It is only suitable for use. No language can completely replace other languages. It makes sense that JAVA has become popular around the world since its appearance. Think a little more,

I have been looking at some things in JAVA recently. I have some experiences in the use of object-oriented languages such as C, PHP, and JAVA over the years. There is no good or bad language. It is only suitable for use. No language can completely replace other languages. It makes sense that JAVA has become popular around the world since its appearance. Think a little more,

I have been looking at some things in JAVA recently. I have some experiences in the use of object-oriented languages such as C ++, PHP, and JAVA over the years.

There is no good or bad language. It is only suitable for use. No language can completely replace other languages.

It makes sense that JAVA has become popular around the world since its appearance.

I think a little more about it. This article focuses on some of my own understandings of synchronized in the Singleton mode written by a colleague in the current project.

First write the code,

public class DBConnPool{private static DBConnPool instance = null;public synchronized static DBConnPool getInstance() {if (instance == null) {try {String modbase = System.getenv("DM_HOME");modbase = modbase != null ? modbase : ".";String dbcfg = modbase + File.separator + "conf" + File.separator + "dbcfg.xml";instance = new DBConnPool(dbcfg);} catch (Exception e) {LOG.fatal("create connection pool instance failed", e);}}return instance;}}


Java synchronized.

We recommend that you use the synchronized block instead of the synchronized method.

public class Singleton{private static volatile Singleton _instance;public static Singleton getInstance(){   if(_instance == null){            synchronized(Singleton.class){              if(_instance == null)              _instance = new Singleton();            }   }   return _instance;}


In this way, in a multi-threaded environment, access to getInstance will not be blocked, but will only block other threads when _ instance is null and needs to be created.

This improves the system efficiency.

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.