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

  • ThreadPoolTaskExecutor config

    I'm trying to configure a ThreadPoolTaskExecutor (java5 variant) to have the following behaviour:

    * One and only one thread in the pool
    * No queue capacity
    * If a task is submitted to the executor when the thread is already busy, then the caller is blocked until the thread is available.

    The first two I can handle, but I can't manage the third. It always throws a RejectedExecutionException instead of blocking.

    I'm not sure what I'm missing, but here's my test case below.

    How do I reconfigure to block instead of failing?

    PHP Code:
    public class ExecutorTest extends TestCase {

        private 
    ThreadPoolTaskExecutor taskExecutor;
        
        @
    Override
        
    protected void setUp() throws Exception {
            
    taskExecutor = new ThreadPoolTaskExecutor();
            
    taskExecutor.setMaxPoolSize(1);
            
    taskExecutor.setQueueCapacity(0);
            
    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
            
    taskExecutor.afterPropertiesSet();
        }
        
        @
    Override
        
    protected void tearDown() throws Exception {
            
    taskExecutor.shutdown();
        }
        
        public 
    void test() {
            
    taskExecutor.execute(new Task());
            
    taskExecutor.execute(new Task());  // this throws an exception
        
    }
        
        class 
    Task implements Runnable {
            public 
    void run() {
                try {
                    
    Thread.sleep(1000);
                } catch (
    InterruptedException ex) {
                    
    ex.printStackTrace();
                }
            }
        }


  • #2
    I can't give you an answer to the Spring variant of the Executor, but I can give you an answer to the ThreadPoolExecutor from Java 5. If you are using Java 5, I would drop the Spring functionality.

    Originally posted by skaffman View Post
    I'm trying to configure a ThreadPoolTaskExecutor (java5 variant) to have the following behaviour:

    * One and only one thread in the pool
    Set the poolsize and the maximum poolsize to 1.

    * No queue capacity
    Use a synchronousqueue as workqueue.

    * If a task is submitted to the executor when the thread is already busy, then the caller is blocked until the thread is available.
    Not possible with normal configuration. But what you can do is create your own RejectedExecutionHandler and after the execution fails, access the workqueue of the ThreadPoolExecutor and do a put instead of an offer. See the ScheduledThreadPoolExecutor for more information (they also access the workqueue directly).

    Comment

    Working...
    X