Yes, Java static initializers are thread safe (use your first option).
However, if you want to ensure that the code is executed exactly once you need to make sure that the class is only loaded by a single class-loader. Static initialization is performed once per class-loader.
More Related Contents:
- How can I get my threaded program to print specific output
- Loop doesn’t see value changed by other thread without a print statement
- What is the difference between a static and a non-static initialization code block
- Avoid synchronized(this) in Java?
- Is multi-thread output from System.out.println interleaved
- Synchronizing on String objects in Java
- Volatile vs Static in Java
- Synchronization vs Lock
- What is the difference between atomic / volatile / synchronized?
- How to synchronize a static variable among threads running different instances of a class in Java?
- Are static variables shared between threads?
- How do I make my ArrayList Thread-Safe? Another approach to problem in Java?
- How do synchronized static methods work in Java and can I use it for loading Hibernate entities?
- Java double checked locking
- Java Synchronized Block for .class
- Static versus non-static lock object in synchronized block
- Java Singleton and Synchronization
- Printing Even and Odd using two Threads in Java
- Waiting on multiple threads to complete in Java
- What is the difference between a synchronized method and synchronized block in Java? [duplicate]
- Are non-synchronised static methods thread safe if they don’t modify static class variables?
- Sharing a variable between multiple different threads
- Program hangs if thread is created in static initializer block
- Should you synchronize the run method? Why or why not?
- Thread safety of static blocks in Java
- Is synchronization within an HttpSession feasible?
- Static variables and multithreading in java
- Why do I need to synchronize a list returned by Collections.synchronizedList
- Thread Safe singleton class
- Stopping a specific java thread