1

I need to implement Multi Threaded background process. My project is spring , hibernate based I tried

with below code which uses org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor to

perform the below background operation in multi threaded manner.I need to know why my

thread count always 1 ?

public class UserUpdateProcessor implements InitializingBean {
private ThreadPoolTaskExecutor executor;

    public void afterPropertiesSet() throws Exception {  
      for(int i = 0; i < 10)    //added this like after  the 1st reply     
        executor.execute(new UserBackgorundRunner ());
    }
 }

  private class UserBackgorundRunner extends Thread {

    public UserBackgorundRunner() {
       this.setDaemon(true);
       this.setPriority(MIN_PRIORITY);
    }
    public void run() {
       List<User> users = getUserList();;

     for (User user : users) {
    try {
   log.debug("Active count :::::::::::::::::::::::::::::"+executor.getActiveCount());
        upgradeUserInBackground(user);
    } catch (Exception e) {
        LOGGER.warn("Fail to upgrade user");
    }
}

}

 My spring.xml looks like 
<bean id="userThreadPool"   
 class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">      
  <property name="corePoolSize"><value>10</value></property>
  <property name="maxPoolSize"><value>15</value></property>
  <property name="queueCapacity"><value>50</value></property>
 </bean>


<bean id="userProcessor" class="com.user.UserUpdateProcessor"
autowire="byType">
<property name="executor" ref="userThreadPool" />
 </bean>

1 Answers1

0

It is always one because you only ever submit a single Thread to the ThreadPoolTaskExecutor.

Spring's InitializingBean (JavaDoc link) method afterPropertiesSet() is only invoked once in the Applications lifetime, and as far as I can tell from the example you have provided, that is the only thing submitting Thread's to your ThreadPoolTaskExecutor.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • I must execute my task inside afterPropertySet method . Do u recommend to add a loop as above to make it multi threaded ? – user3701829 Jun 05 '14 at 01:48
  • I do not recommend that. The way it is now, your task is indeed being executed in the background, and it is running concurrently with the main application thread. In that sense, you are running 'multi-threaded'. It really depends on what the requirements of your task are. Can you perhaps provide some more information? – nicholas.hauschild Jun 05 '14 at 01:58
  • When the application starts up I need to update user table's encrypted information by the system in a multithreaded manner. The reason to make it multi threaded 1. I prefer do this sooner. (But i have to handle synchronizations isssues in that case which is a problem too.) What is the reason for not recommending the looping way ? Any specific reason ? – user3701829 Jun 05 '14 at 02:09
  • nicholas Please let me know if the provided details are not adequate. Thanks – user3701829 Jun 05 '14 at 02:13
  • I need to make this update when the application server starts up depending on a value of a property file. This value checks inside the afterPropertySet() mehtod if it is true I should start the background threads to do this update and complete it as Soon as possible in background. – user3701829 Jun 05 '14 at 02:26
  • What exactly needs to be multithreaded here? Can each user be updated individually in a separate thread? Like I said, you are currently accomplishing this work in a background thread. If you want to update each thing individually, you will have to do so with separate Thread objects. – nicholas.hauschild Jun 05 '14 at 03:23
  • There are more than 1 millions of users in the table. If I update with a single thread it takes long to to do the task. How to start the separate thread objects and any particular reason u think to bad use a Spring looping executors ? – user3701829 Jun 05 '14 at 03:44
  • Good thing in the current background I was expecting synchronization issues with multiple threads. But so far I did not see them may be due to 1 active thread – user3701829 Jun 05 '14 at 03:49