Core role:

– Ensures that a class has only one instance and provides a global access point to that instance

The computer manager, once created a task manager, to create the same again.

 

 

 

 

1. Singleton mode: Hangry.

 

Hungry: The same object no matter how many objects are created

 

 

Two, lazy: with delayed loading. Add synchronization, call efficiency is low.

 

 

Double check locks: Occasionally problems occur due to compiler optimizations and JVM underlying internal models

Not recommended.

 

Four: static inner class pattern

Advantages: 1 thread safety, 2 call efficiency, 3 lazy loading

/** * The external class has no static property, so it does not load the object immediately. ** * The external class has no static property, so it does not load the object immediately. ** * The external class has no static property, so it does not load the object immediately. And can only be assigned once, thus ensuring thread safety * with the advantages of concurrent efficient calls and slow loading. * * /

 

Enumerative singleton pattern

The enumeration element itself is a singleton, thread-safe,

Natural protection against reflection and deserialization vulnerabilities

But there is no delayed loading

 

 

 

 

 

 

 

 

 

Test time: