Threadsafe static initialization of ExecutorService

Viewed 1307

I am trying to create a threadsafe singleton class based on Initialization-on-demand holder idiom . Here is my code

public class Check{ 
  private Check(){  }
  private static class Provider {
    static final ExecutorService INSTANCE = new ThreadPoolExecutor(5, "read this val from file", 60L, TimeUnit.SECONDS, new LinkedBlockingQueue());
  }
  public static ExecutorService getInstance() {
    return Provider.INSTANCE;
  }
}

My expectation is to initialize ExecutorService in a threadsafe manner and only one instance should be there (static).

Is this code achieving that - or are any changes required?

2 Answers
Related