I haven’t written an article for a long time, because I signed up for a architecture course, and recently I also spent time in learning, learning all kinds of source code.

In source-level projects, singletons are used so often that one might say that singletons are out of date, as any developer knows.

Let me ask you a few questions:

1. How many ways to write singletons? The more the better?

2. How to write a thread-safe singleton pattern?

The answers to these questions can be found in the following passage. Good analysis.

Dive into the Java singleton pattern

But with regard to the last piece of thread-safety, the source code level is usually Double Check + volatile, which is more convenient than static inner classes.

Why volatile variables are used is also unclear in the previous article, but volatile mainly ensures visibility and order in the memory model of variables.

The specific content, I will write concurrent related articles when I have time later, but it should be next year.

If you want to learn more about volatile, you are recommended to study the interview series, as well as the JVM, MySQL, and Rocket MQ columns. If you have any questions, you can pay attention to my public number: Xiao Li is not bald, as long as there is time will be unified reply.