国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring線程池ThreadPoolExecutor配置并且得到任務執(zhí)行的結果

Spring線程池ThreadPoolExecutor配置并且得到任務執(zhí)行的結果

2021-07-21 11:37paulwong Java教程

今天小編就為大家分享一篇關于Spring線程池ThreadPoolExecutor配置并且得到任務執(zhí)行的結果,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

用threadpoolexecutor的時候,又想知道被執(zhí)行的任務的執(zhí)行情況,這時就可以用futuretask。

threadpooltask

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.paul.threadpool;
import java.io.serializable;
import java.util.concurrent.callable;
public class threadpooltask implements callable<string>, serializable {
  private static final long serialversionuid = 0;
  // 保存任務所需要的數據
  private object threadpooltaskdata;
  private static int consumetasksleeptime = 2000;
  public threadpooltask(object tasks) {
    this.threadpooltaskdata = tasks;
  }
  public synchronized string call() throws exception {
    // 處理一個任務,這里的處理方式太簡單了,僅僅是一個打印語句
    system.out.println("開始執(zhí)行任務:" + threadpooltaskdata);
    string result = "";
    // //便于觀察,等待一段時間
    try {
//      long r = 5/0;
      for ( int i= 0 ; i< 100000000 ; i++){ 
      }
      result = "ok";
    } catch (exception e) {
      e.printstacktrace();
      result = "error";
    }
    threadpooltaskdata = null;
    return result;
  }
}

模擬客戶端提交的線程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.paul.threadpool;
import java.util.concurrent.executionexception;
import java.util.concurrent.futuretask;
import org.springframework.scheduling.concurrent.threadpooltaskexecutor;
public class starttaskthread implements runnable{
 private threadpooltaskexecutor threadpooltaskexecutor;
 private int i;
 public starttaskthread(threadpooltaskexecutor threadpooltaskexecutor,int i)
 {
 this.threadpooltaskexecutor = threadpooltaskexecutor;
 this.i = i;
 }
 @override
 public synchronized void run() {
 string task = "task@ " + i;
 system.out.println("創(chuàng)建任務并提交到線程池中:" + task);
 futuretask<string> futuretask = new futuretask<string>(
 new threadpooltask(task));
 threadpooltaskexecutor.execute(futuretask);
 // 在這里可以做別的任何事情
 string result = null;
 try {
 // 取得結果,同時設置超時執(zhí)行時間為0.1秒。同樣可以用future.get(),不設置執(zhí)行超時時間取得結果
 result = futuretask.get();
 } catch (interruptedexception e) {
 futuretask.cancel(true);
 } catch (executionexception e) {
 futuretask.cancel(true);
 } catch (exception e) {
 futuretask.cancel(true);
 // 超時后,進行相應處理
 } finally {
 system.out.println("task@" + i + ":result=" + result);
 }
}

spring配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemalocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 <!-- 配置數據源 -->
 <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"
 destroy-method="close" p:driverclassname="com.mysql.jdbc.driver"
 p:url="jdbc:mysql://localhost:3306/mb_main?useunicode=true&characterencoding=utf-8&useserverprepstmts=true" p:username="root" p:password="1234" />
 <!-- 配置jdbc模板 -->
 <bean id="jdbctemplate" class="org.springframework.jdbc.core.jdbctemplate"
 p:datasource-ref="datasource" />
 <!-- 事務管理器 -->
 <bean id="transactionmanager"
 class="org.springframework.jdbc.datasource.datasourcetransactionmanager"
 p:datasource-ref="datasource" />
 <tx:advice id="jdbctxadvice" transaction-manager="transactionmanager">
 <tx:attributes>
 <tx:method name="*" />
 </tx:attributes>
 </tx:advice>
 <!-- 使用aop/tx命名空間配置事務管理,這里對service包下的服務類方法提供事務 -->
 <aop:config>
 <aop:pointcut id="jdbcservicemethod" expression="within(com.baobaotao.service..*)" />
 <aop:advisor pointcut-ref="jdbcservicemethod" advice-ref="jdbctxadvice" />
 </aop:config>
 <!-- 配置dao
 <bean id="loginlogdao" class="com.baobaotao.dao.loginlogdao"
 p:jdbctemplate-ref="jdbctemplate" />
 <bean id="userdao" class="com.baobaotao.dao.userdao"
 p:jdbctemplate-ref="jdbctemplate" />
 <bean id="userservice" class="com.baobaotao.service.userservice"
 p:userdao-ref="userdao" p:loginlogdao-ref="loginlogdao" />
 -->
 <bean id="threadpooltaskexecutor" class="org.springframework.scheduling.concurrent.threadpooltaskexecutor">
 <!-- 核心線程數,默認為1 -->
 <property name="corepoolsize" value="10" />
 <!-- 最大線程數,默認為integer.max_value -->
 <property name="maxpoolsize" value="50" />
 <!-- 隊列最大長度,一般需要設置值>=notifyscheduledmainexecutor.maxnum;默認為integer.max_value
 <property name="queuecapacity" value="1000" />
 -->
 <!-- 線程池維護線程所允許的空閑時間,默認為60s -->
 <property name="keepaliveseconds" value="300" />
 <!-- 線程池對拒絕任務(無線程可用)的處理策略,目前只支持abortpolicy、callerrunspolicy;默認為后者 -->
 <property name="rejectedexecutionhandler">
 <!-- abortpolicy:直接拋出java.util.concurrent.rejectedexecutionexception異常 -->
 <!-- callerrunspolicy:主線程直接執(zhí)行該任務,執(zhí)行完之后嘗試添加下一個任務到線程池中,可以有效降低向線程池內添加任務的速度 -->
 <!-- discardoldestpolicy:拋棄舊的任務、暫不支持;會導致被丟棄的任務無法再次被執(zhí)行 -->
 <!-- discardpolicy:拋棄當前任務、暫不支持;會導致被丟棄的任務無法再次被執(zhí)行 -->
 <bean class="java.util.concurrent.threadpoolexecutor$callerrunspolicy" />
 </property>
 </bean>
</beans>

測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.paul.threadpool;
import java.util.concurrent.arrayblockingqueue;
import java.util.concurrent.threadpoolexecutor;
import java.util.concurrent.timeunit;
import org.junit.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.scheduling.concurrent.threadpooltaskexecutor;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.abstractjunit4springcontexttests;
@contextconfiguration
public class testthreadpool extends abstractjunit4springcontexttests{
 private static int producetasksleeptime = 10;
 private static int producetaskmaxnumber = 1000;
 @autowired
 private threadpooltaskexecutor threadpooltaskexecutor;
 public threadpooltaskexecutor getthreadpooltaskexecutor() {
 return threadpooltaskexecutor;
 }
 public void setthreadpooltaskexecutor(
 threadpooltaskexecutor threadpooltaskexecutor) {
 this.threadpooltaskexecutor = threadpooltaskexecutor;
 }
 @test
 public void testthreadpoolexecutor()
 {
 // 構造一個線程池
 final threadpoolexecutor threadpool = new threadpoolexecutor(2, 4, 600,
 timeunit.seconds, new arrayblockingqueue<runnable>(3),
 new threadpoolexecutor.callerrunspolicy());
 for (int i = 1; i <= producetaskmaxnumber; i++) {
 try {
 thread.sleep(producetasksleeptime);
 } catch (interruptedexception e1) {
 e1.printstacktrace();
 }
 new thread(new starttaskthread(threadpooltaskexecutor,i)).start();
 }
 }
}

項目截圖(基于maven構建)

Spring線程池ThreadPoolExecutor配置并且得到任務執(zhí)行的結果

運行截圖:

Spring線程池ThreadPoolExecutor配置并且得到任務執(zhí)行的結果

如果遇到cpu忙執(zhí)行超過1秒的會返回null

Spring線程池ThreadPoolExecutor配置并且得到任務執(zhí)行的結果

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:http://www.blogjava.net/paulwong/archive/2011/12/07/365773.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产精品自产拍在线观看 | 国产黄色大片免费在线观看 | 日本在线免费 | 久久精品国产一区二区三区 | 91小视频| 国产成人欧美一区二区三区的 | 小视频免费在线观看 | 天天舔日日干 | 免费日韩精品 | 欧美激情视频一区二区三区在线播放 | 我和我的祖国电影在线观看免费版高清 | 国产精品国产a | 欧美一区二区三区在线播放 | 亚洲福利在线播放 | 欧美亚洲三级 | 秋霞av国产精品一区 | 欧美精品在线观看 | 99黄色片 | 水卜樱一区二区av | 在线一区观看 | 综合伊人久久 | 在线不卡一区 | 精品一区二区久久久久久久网站 | 成年免费视频 | 亚洲一区视频在线 | 亚洲在线视频播放 | 国产成人精品一区二区三区四区 | 男人的天堂在线免费视频 | 亚洲伦理影院 | 国产在线不卡 | 午夜视频在线 | 亚洲视频中文字幕在线观看 | 一区二区三区国产 | 成人免费在线电影 | 91中文字幕在线 | 日韩一区精品 | 91久久久久久久久 | 一级黄色片在线 | 精品9999| 免费啪啪av乱一区 | 免费av在线播放 |