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.