I created a singleton to have a unique instance of ExecutorService in my application.
I whould like to know which is the better of the two classes below.
First one :
public class ExecutorServiceSingleton {
private static final Logger LOG = LoggerFactory.getLogger(ExecutorServiceSingleton.class);
private ScheduledExecutorService scheduledExecutorService;
private static ExecutorServiceSingleton instance;
private ExecutorServiceSingleton() {
scheduledExecutorService = Executors.newScheduledThreadPool(1);
}
/**
* Method to run the task
*/
public void runTask(Runnable task) {
scheduledExecutorService.submit(task);
}
/**
* Get the instance : If the instance is null, then we return a new one. If the instance is diff than null, then, we use the instance value and we return it.
*
* @return the instance
*/
public static ExecutorServiceSingleton getInstance() {
if (instance == null) {
instance = new ExecutorServiceSingleton();
}
LOG.info("Instance: " + instance);
return instance;
}
}
Second one :
public class ExecutorServiceSingleton {
private static final Logger LOG = LoggerFactory.getLogger(ExecutorServiceSingleton.class);
private ScheduledExecutorService scheduledExecutorService;
private volatile static ExecutorServiceSingleton instance;
private ExecutorServiceSingleton() {
scheduledExecutorService = Executors.newScheduledThreadPool(1);
}
/**
* Method to run the task
*/
public void runTask(Runnable task) {
scheduledExecutorService.submit(task);
}
/**
* Get the instance : If the instance is null, then we return a new one. If the instance is diff than null, then, we use the instance value and we return it.
*
* @return the instance
*/
public static ExecutorServiceSingleton getInstance() {
if (instance == null) {
synchronized (ExecutorServiceSingleton.class) {
if (instance == null) {
instance = new ExecutorServiceSingleton();
}
}
}
LOG.info("Instance: " + instance);
return instance;
}
}
final static
guarantees in the first place. \$\endgroup\$