3

In my spring based application we are initializing threadPool using ExecutorService for concurrent task execution.We don't want the threadPool to be initialised multiple times in case of multithreaded environment, for which we are making our factory class as Singleton. below is the factory implementation which we are using for threadPool initialization. Sonar analysis is giving a Bug warning as "Remove the "volatile" keyword from this field" for below line- private static volatile ExecutorService executor; is it required to make our ExecutorService as Volatile to achieve above behavior. Below is the factory class

public class ThreadPoolExecutorFactory {

    /** The executor. */
    private static volatile ExecutorService executor;

    /**
     * Instantiates a new thread pool executor factory.
     */
    private ThreadPoolExecutorFactory() {
        // preventing Singleton object instantiation from outside
    }

    /**
     * Gets the executor.
     *
     * @return the executor
     */
    public static ExecutorService getExecutor( ) {
        if(executor == null) {
            synchronized (ThreadPoolExecutorFactory.class) {
                if(executor == null) {
                    executor = Executors.newCachedThreadPool();
                    Runtime.getRuntime().addShutdownHook(new Thread() {
                        @Override
                        public void run() {
                            executor.shutdown();
                        }
                    });
                }
            }
        }
        return executor;
    }
}
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
Ankit
  • 81
  • 2
  • 10
  • 2
    As you're using Spring, why not make your factory class a spring component and create the executor in its constructor? – tgdavies Sep 16 '19 at 11:05
  • @tgdavies what if the same scenario we want to achieve in java without using spring. is volatile mandatory on ```ExecutorService``` ? – Ankit Sep 17 '19 at 17:27

1 Answers1

0

I think you are mixing Java standalone multi-threading concepts with Spring. In Spring, you can define a class as a "Bean" with "singleton" scope (default). You can have an init method and a destroy method which are guaranteed to be called.

In applicationContext.xml, define bean as below:

    <bean id="myExecutor" class="my.package.factories.ThreadPoolExecutorFactory"
                        scope="singleton"
                        init-method = "init"
                        destroy-method = "destroy">

And change class as below:

package my.package.factories

public class ThreadPoolExecutorFactory {

    /** The executor. */
    private static ExecutorService executor;


    public void init() {
        executor = Executors.newCachedThreadPool();
    }

    public void destroy() {
        executor.shutdown();
    }

    /**
     * Gets the executor.
     *
     * @return the executor
     */
    public static ExecutorService getExecutor( ) {
        return executor;
    }
}

Spring will take care rest of the details.

bkbb
  • 237
  • 1
  • 6
  • Default Scope is Singleton so no need to explicit make Singleton . – algomadesimple Sep 17 '19 at 09:20
  • 1
    @bytingbee we can use the spring singleton scope but suppose same thing i want in java using singleton factory then is the volatile keyword is required on ```ExecutorService``` ? – Ankit Sep 17 '19 at 17:24
  • If double checking locking is used (the way you implemented singleton class) then volatile is needed. Alternatively you can make getExecutor method synchronized and get rid of volatile keyword. If lazy initialization is not required then you could have another class as helper where singleton object can be defined as 'final'. Refer to HelperSingleton in http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html – bkbb Sep 17 '19 at 18:07