How does the JVM enforce Singleton threading security enumerated?

According to Josh Bloch Efficient Java Enum is the best way to implement the Singleton pattern.

The Java enum takes care of Serialization and Thread Safety on its own. But I do not understand how.

Can someone explain this?

I looked over this and this .

The second link gives a good amount of detail that the enumeration is serializable, but I don't get enough explanation of thread safety.

Also, are the methods declared in the enumeration thread safe or require special care?

+3
source share
2 answers

static JVM. jstack, . - . , enum.

enum , .

, , , ?

, enum . , synchronized. : .;)

enum static final, , .. final . , Unsafe.allocateInstance. , .

JLS 8.9.2

n, n E. , , , - , .

+4

Enum - , . / . . Enum .

, enum, , .

+1

All Articles