Announcement Announcement Module
Collapse
No announcement yet.
Help with TaskExecuter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with TaskExecuter

    I am having some issues understanding how exactly the TaskExecuter works.

    I am trying to follow the example in 25.2.2 of the docs.

    It would seem that every time the printMessage method is called a new instance of the class MessagePrinterTask is created. Because the example uses ThreadPoolTaskExecutor a thread is reused from the pool to run the task. Correct?

    Also, will the call to .execute() block if there are more calls in queue then what is defined in queueCapacity?


    Code:
    import org.springframework.core.task.TaskExecutor;
    
    public class TaskExecutorExample {
    
      private class MessagePrinterTask implements Runnable {
    
        private String message;
    
        public MessagePrinterTask(String message) {
          this.message = message;
        }
    
        public void run() {
          System.out.println(message);
        }
    
      }
    
      private TaskExecutor taskExecutor;
    
      public TaskExecutorExample(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
      }
    
      public void printMessages() {
        for(int i = 0; i < 25; i++) {
          taskExecutor.execute(new MessagePrinterTask("Message" + i));
        }
      }
    }

  • #2
    Hello

    Spring's org.springframework.scheduling.concurrent.ThreadPo olTaskExecutor uses standard Java 5 java.util.concurrent packages (let's not talk about backport-util-concurrent right now), especially java.util.concurrent.ThreadPoolExecutor class.

    About your question. Every time printMessages() is called, 25 instances of MessagePrinterTask are created. This is direct application of Object Oriented principles - you need to perform a task? Create one - don't use the one which was already executed.

    And about ThreadPoolExecutor configuration. Because corePoolSize is 5 and maxPoolSize is 10, the first five execute() invocations will lead to creating new Threads which will be added to pool. The sixth task:
    • will be added to queue if the first five tasks haven't finished yet
    • will be executed by one of the first 5 threads otherwise

    This example can't fill the queue, but let's say that first five threads are still running (executing MessagePrinterTask.run() methods) and the queue is full (holds 25 tasks). Running execute() one more time will cause creating sixth thread, adding it to pool and using it to execute the new task.
    I hope this explains you how it works. For all the details see: http://download.oracle.com/javase/6/...lExecutor.html.

    regards
    Grzegorz Grzybek

    Comment


    • #3
      Based on the spring config below, what happens when queueCapacity is exceeded? Will the thread trying to place something into the queue be blocked or will an exception be thrown? I am guessing that Spring is rewrapping functionality in the java6 version of ThreadPoolExecutor, which has several options for rejected messages, what does Spring use for the default?


      Code:
      <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="5" />
        <property name="maxPoolSize" value="10" />
        <property name="queueCapacity" value="25" />
      </bean>
      
      <bean id="taskExecutorExample" class="TaskExecutorExample">
        <constructor-arg ref="taskExecutor" />
      </bean
      Last edited by nedry; Mar 21st, 2011, 07:51 AM.

      Comment


      • #4
        Hi

        If corePoolSize < maxPoolSize, new Thread is created, added to pool and used to execute passed task. Otherwise there's something called "RejectedExecutionHandler" - one particular implementation may be used to use "caller runs policy" - the execute() method (and run()) will be called in the same thread.

        regards
        Grzegorz Grzybek

        Comment


        • #5
          Originally posted by Grzegorz Grzybek View Post
          Hi

          If corePoolSize < maxPoolSize, new Thread is created, added to pool and used to execute passed task. Otherwise there's something called "RejectedExecutionHandler" - one particular implementation may be used to use "caller runs policy" - the execute() method (and run()) will be called in the same thread.

          regards
          Grzegorz Grzybek
          How do you change the policy via spring? I think I want to use ThreadPoolExecutor.DiscardOldestPolicy

          Comment


          • #6
            Hi

            use:
            Code:
            <bean class="org.springframework.scheduling.backportconcurrent.ThreadPoolTaskExecutor">
              <property name="rejectedExecutionHandler">
                <bean class="java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy" />
              </property>
            </bean>
            regards
            Grzegorz Grzybek

            Comment


            • #7
              Thanks, I think I have got what I need now.

              Comment

              Working...
              X