Announcement Announcement Module
Collapse
No announcement yet.
using ThreadPoolExecutor in spring, may have a bug Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • using ThreadPoolExecutor in spring, may have a bug

    Code:
    package com.tencent.xxx;
     
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
     
    /**
     * @author <a href='mailto:[email protected]'>Bruce</a>
     * @version 1.0, Mar 19, 2010 2:03:14 AM
     */
    public class ThreadPacketReader {
     
        private ThreadPoolExecutor threadPool;
     
        public ThreadPacketReader() {
            // TODO Auto-generated constructor stub
            int coreThreads = Integer.parseInt(System.getProperty("com.tencent.xxx.routing.core.threads", "2"));
            int maxThreads = Integer.parseInt(System.getProperty("com.tencent.xxx.routing.max.threads", "50"));
            int queueSize = Integer.parseInt(System.getProperty("com.tencent.xxx.routing.queue", "1"));
     
            threadPool = new ThreadPoolExecutor(coreThreads, maxThreads, 60, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(queueSize), new ThreadPoolExecutor.AbortPolicy());
        }
     
        public void process() {
            threadPool.execute(new Runnable() {
                public void run() {
     
                    System.err.println("----------" + threadPool.getPoolSize() + " : " + threadPool.getActiveCount()
                            + " : " + threadPool.getTaskCount());
                    
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
      <bean id="threadPacketReader" class="com.tencent.xxx.ThreadPacketReader"></bean>
     
    </beans>
    Code:
    @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
    public class ThreadPoolTests extends AbstractTestNGSpringContextTests {
     
        @Resource
        private ThreadPacketReader threadPacketReader;
     
        @Test
        public void poolSize_accuracy() {
            while (true) {
     
                threadPacketReader.process();
     
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
     
            }
        }
     
    }
    The Result:
    the
    Code:
    activeCode
    is below 10. i did not find it happen without spring.

    Code:
    ----------47 : 2 : 11260
    ----------47 : 2 : 11261
    ----------47 : 1 : 11262
    ----------47 : 2 : 11263
    ----------47 : 2 : 11264
    ----------47 : 2 : 11265
    ----------47 : 3 : 11266
    ----------47 : 3 : 11267
    ......
    ----------47 : 2 : 11449
    ----------47 : 3 : 11450
    ----------47 : 2 : 11451
    ----------47 : 2 : 11452
    ----------47 : 2 : 11453
    ----------47 : 2 : 11454
    ----------47 : 2 : 11455
    ----------47 : 2 : 11456
    ----------47 : 3 : 11457
    ----------47 : 2 : 11458
    ----------47 : 2 : 11459
    ----------47 : 2 : 11460
    ----------47 : 2 : 11461
    ----------47 : 3 : 11462
    ----------47 : 2 : 11463
    ----------47 : 2 : 11464
    ----------47 : 2 : 11465
    ----------47 : 3 : 11466
    ----------47 : 2 : 11467
    ----------47 : 2 : 11468
    ----------47 : 2 : 11469
    ----------47 : 2 : 11470
    ----------47 : 3 : 11471
    ----------47 : 2 : 11472
    ----------47 : 2 : 11473
    ----------47 : 2 : 11474
    ----------47 : 2 : 11475
    ----------47 : 2 : 11476
    ----------47 : 2 : 11477
    ----------47 : 3 : 11478
    ----------47 : 2 : 11479
    ----------47 : 2 : 11480
    ----------47 : 2 : 11481
    ----------47 : 2 : 11482
    ----------47 : 2 : 11483
    ----------47 : 2 : 11484
    ----------47 : 2 : 11485
    ----------47 : 2 : 11486
    ----------47 : 2 : 11487
    ----------47 : 2 : 11488
    ----------47 : 2 : 11489
    ----------47 : 2 : 11490
    ----------47 : 2 : 11491
    ----------47 : 2 : 11492
    ----------47 : 2 : 11493
    ----------47 : 2 : 11494
    ----------47 : 2 : 11495
    ----------47 : 2 : 11496
    ----------47 : 2 : 11497
    ----------47 : 2 : 11498
    ----------47 : 2 : 11499
    ----------47 : 2 : 11500
    .......
    ----------50 : 2 : 11669
    ----------50 : 3 : 11670
    ----------50 : 4 : 11671
    ----------50 : 4 : 11672
    ----------50 : 2 : 11673
    FAILED: poolSize_accuracy
    java.util.concurrent.RejectedExecutionException
      at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1964)
      at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:816)
      at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1337)
      at com.tencent.xxx.ThreadPacketReader.process(ThreadPacketReader.java:29)
      at com.tencent.xxx.ThreadPoolTests.poolSize_accuracy(ThreadPoolTests.java:27)
      at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:140)
    ... Removed 28 stack frames

  • #2
    All I see is a rejected execution exception - this happens when no: of threads > pool size - I am not sure what you are planning to accomplish, but if you want to queue the thread and retry later you should use a CallerRuns policy.

    Comment


    • #3
      There may be something to this post because...

      I am trying to upgrade some old code from Spring 1.2.6 to 3.0.5 and I get the same error in 3.0.5 where there used to be no error. I'm not saying there is a bug but if there is a better way or a make Spring play nice with ThreadPoolExectuor, I'd be happy to learn.

      Comment

      Working...
      X