Singleton instantiation

前端 未结 10 1590
我寻月下人不归
我寻月下人不归 2020-12-05 05:40

Below show is the creation on the singleton object.

public class Map_en_US extends mapTree {

    private static Map_en_US m_instance;

    private Map_en_US         


        
10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 06:06

    If you initialize in the getInstance() method, you can get a racing conditions, i.e. if 2 threads execute the if(m_instance == null) check simulataneously, both might see the instance be null and thus both might call m_instance = new Map_en_US();

    Since the static initializer block is executed only once (by one thread that is executing the class loader), you don't have a problem there.

    Here's a good overview.

提交回复
热议问题