Search code examples
javaenumsthread-safety

Enum methods thread-safety


There is a notion that "singleton implementation of enum is thread-safe by default" which I found in this article at javatpoint.com.

If that is the case then does this mean that any method which I write myself inside of this enum singleton class is thread-safe as well without the need to use any additional keywords/logic to make it thread-safe?


Solution

  • No, that is not what it means. It just means that the creation and initialization of a singleton object by implementing it as an enum with one value is thread-safe, because of how class-loading works in Java. It means no more, and no less.

    It does not mean that the implementation of your enum itself is somehow thread-safe if you don't program it in a way that is threadsafe.