關於Spring封裝Java線程池的理解__線程池

來源:互聯網
上載者:User

項目用到了TaskExecutor,我看有的是通過spring bean配置的.有的是通過配置的方式,於是看了下官方文檔,官方文檔(好像是第30.5章)上寫的很清楚,裡面的參數的含義等東東,

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"><!-- 核心線程數 --> <property name="corePoolSize" value="${task.core_pool_size}" /><!-- 最大線程數 -->  <property name="maxPoolSize" value="${task.max_pool_size}" /><!-- 隊列最大長度 --><property name="queueCapacity" value="${task.queue_capacity}" /><!-- 線程池維護線程所允許的空閑時間,預設為60s --><property name="keepAliveSeconds" value="${task.keep_alive_seconds}" /></bean><!-- 註解式 --><task:annotation-driven /><!-- 或則 -->   <task:executor id="asyncTaskExecutor"                   pool-size="${executor.pool.size}"                   queue-capacity="${executor.queue.capacity}"                   keep-alive="500"/>

至於SpringMVC整合TaskExecutor線程池的配置/使用
一、配置jdbc.properties添加:

#------------ Task ------------task.core_pool_size=5task.max_pool_size=50task.queue_capacity=1000task.keep_alive_seconds=60

二、配置Spring的applicationContext.xml添加:
  
就是上面的配置

三、在實現層Impl聲明並注入:

       

@Resource(name = "taskExecutor")    private TaskExecutor taskExecutor;

四、在方法裡調用:

try {taskExecutor.execute(new Runnable() {public void run() {//這裡編寫處理業務代碼}});} catch (Exception e) {e.printStackTrace();}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.