Singleton mode of Java multithreaded programming

Deferred loading means that an instance is created when the getInstance () method is called. A common method is to instantiate new in the getInstance () method. The implementation code is as follows:

However, because there are multiple statements in getInstance (), there may be thread safety problems. The operation results also show that:

Even if there are more statements in getInstance (), there will be three different objects. Thread (3000) is added to the if (MyObject = = null) statement block. The results show that:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>