Cyh的博客
Email:kissyan4916@163.com
posts - 26, comments - 19, trackbacks - 0, articles - 220
導(dǎo)航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
公告
一直努力努力努力,像奴隸奴隸奴隸!~~
<
2025年5月
>
日
一
二
三
四
五
六
27
28
29
30
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
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
隨筆檔案
(25)
2011年5月 (1)
2010年4月 (12)
2010年1月 (1)
2009年12月 (2)
2009年6月 (1)
2009年4月 (4)
2009年2月 (4)
文章分類
(219)
Android(26)
DB(5)
J2EE(31)
J2SE(79)
JavaScript(15)
others(47)
SOA&Web Service(1)
中間件(1)
軟件工程(12)
軟件架構(gòu)(2)
文章檔案
(220)
2011年8月 (1)
2010年12月 (23)
2010年11月 (2)
2010年8月 (5)
2010年7月 (2)
2010年6月 (2)
2010年5月 (1)
2010年4月 (12)
2010年3月 (28)
2010年2月 (5)
2010年1月 (23)
2009年12月 (39)
2009年6月 (14)
2009年5月 (31)
2009年3月 (2)
2009年2月 (29)
2009年1月 (1)
新聞檔案
(66)
2010年10月 (1)
2010年9月 (5)
2010年8月 (11)
2010年7月 (21)
2010年6月 (13)
2010年5月 (8)
2010年4月 (5)
2009年11月 (2)
相冊
Ryan
收藏夾
(7)
JAVA(7)
最新隨筆
1.?集成FCKeditor 3.5.3
2.?android自適應(yīng)屏幕方向和大小
3.?Android游戲開發(fā)之旅(二十) 雙按事件捕獲
4.?Android游戲開發(fā)之旅(十八) SoundPool類
5.?Android游戲開發(fā)之旅(十九) 分辨率大全
6.?Android游戲開發(fā)之旅(十七) 圖像漸變特效
7.?Android游戲開發(fā)之旅(十六) 異步音樂播放
8.? Android游戲開發(fā)之旅(十四) 游戲開發(fā)實(shí)戰(zhàn)一
9.?Android游戲開發(fā)之旅(十五) 按鍵中斷處理
10.?Android游戲開發(fā)之旅(十二)Sensor重力感應(yīng)(2)
搜索
最新評論
1.?re: struts2 checkboxlist標(biāo)簽的使用
同居同意同意
--yuk
2.?re: struts2 checkboxlist標(biāo)簽的使用
ss
--d
3.?re: JavaMail(4)--使用POP3接收郵件
郵件信息可以打印出來,可是下載郵件會出錯是什么原因?
--琳喵喵0721
4.?re: JavaMail(4)--使用POP3接收郵件
評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--流風(fēng)
5.?re: 操作PDF文件
評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--ly.wolf
閱讀排行榜
1.?struts2 checkboxlist標(biāo)簽的使用(18234)
2.?struts2異常攔截器(5862)
3.?struts2迭代標(biāo)簽(3847)
4.?用freemind 秒殺Spring Security(1920)
5.?加載順序會影響對spring bean 的調(diào)用。(1491)
線程--任務(wù)執(zhí)行架構(gòu)
Posted on 2009-12-22 20:53
啥都寫點(diǎn)
閱讀(557)
評論(0)
編輯
收藏
所屬分類:
J2SE
Executor服務(wù)對象是用來執(zhí)行Runnable任務(wù)的,常用的方法如下:
execute方法用于執(zhí)行Ruannable類型的任務(wù)。
ExecutorService服務(wù)對象能執(zhí)行和終止Callable任務(wù),它繼承了Executor,所以也能執(zhí)行Runnable任務(wù)。常用的方法如下:
submit方法用來提交Callable 或Runnable任務(wù),并返回代表此任務(wù)的Future對象。
invokeAll方法批處理任務(wù)集合,并返回一個代表這些任務(wù)的Future對象集合。
shutdown方法在完成已提交的任務(wù)后關(guān)閉服務(wù),不再接受新任務(wù)。
shutdownNow方法停止所有正在執(zhí)行的任務(wù)并關(guān)閉服務(wù)。
isTerminated測試是否所有任務(wù)都執(zhí)行完畢了。
isShuttdown測試是否該ExecutorService已被關(guān)閉。
ScheduledExecutorService服務(wù)對象繼承ExecutorService,提供了按時間安排執(zhí)行任務(wù)的功能。常用的方法如下:
a>schedule(task,initDelay)方法安排所提交的Runnable任務(wù)按指定的間隔重復(fù)執(zhí)行。
b>scheduleAtFixedRate方法安排所提交的Runnable任務(wù)按指定的間隔重復(fù)執(zhí)行。
c>sheduleWithFixedDelay方法安排所提交的Runnable任務(wù)在每次執(zhí)行完后,等待delay所指定的時間后重復(fù)執(zhí)行。
Executors類用來創(chuàng)建各種服務(wù)對象,常用的方法如下:
a> callable(Runnable task)方法將Runnable的任務(wù)轉(zhuǎn)化成Callable的任務(wù)。
b> newSingleThreadExecutor方法產(chǎn)生一個ExecutorService對象,這個對象只有一個線程可用來執(zhí)行任務(wù),若任務(wù)多于一個,則按先后順序執(zhí)行任務(wù)。
c>newCachedThreadPool方法產(chǎn)生一個ExecutorService對象,這個對象帶有一個線程池,線程池的大小會根據(jù)需要調(diào)整,線程執(zhí)行完任務(wù)后返回線程池,供執(zhí)行下一次任務(wù)使用。
d>newFixedThreadPool(int poolSize)方法產(chǎn)生一個ExecutorService對象,這個對象帶有一個大小為poolSize的線程池,若任務(wù)數(shù)量大于poolSize,任務(wù)會被放在一個隊(duì)列里順序執(zhí)行
e> newSingleThreadScheduledExecutor方法產(chǎn)生一個ScheduledExecutorService對象,這個對象的線程池大小為1,若任務(wù)多余一個,任務(wù)將按先后順序執(zhí)行。
f>newScheduledThreadPool(int poolSize) 方法產(chǎn)生一個ScheduleExecutorService對象,這個對象的線程池大小為poolSize,若任務(wù)數(shù)量大于poolSize,任務(wù)會在一個隊(duì)列里等待執(zhí)行。
import
java.util.concurrent.Callable;
import
java.util.concurrent.ExecutionException;
import
java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors;
import
java.util.concurrent.Future;
import
java.util.concurrent.ScheduledExecutorService;
import
java.util.concurrent.TimeUnit;
/** */
/**
* 新的任務(wù)執(zhí)行架構(gòu)。
* 在Java 5.0之前啟動一個任務(wù)是通過調(diào)用Thread類的start()方法來實(shí)現(xiàn)的,
* 任務(wù)的提于交和執(zhí)行是同時進(jìn)行的,如果你想對任務(wù)的執(zhí)行進(jìn)行調(diào)度,
* 或是控制同時執(zhí)行的線程數(shù)量就需要額外編寫代碼來完成。
* 5.0里提供了一個新的任務(wù)執(zhí)行架構(gòu)使你可以輕松地調(diào)度和控制任務(wù)的執(zhí)行,
* 并且可以建立一個類似數(shù)據(jù)庫連接池的線程池來執(zhí)行任務(wù)。
* 這個架構(gòu)主要有三個接口和其相應(yīng)的具體類組成。
* 這三個接口是Executor, ExecutorService和ScheduledExecutorService。
* (1)Executor接口:是用來執(zhí)行Runnable任務(wù)的,它只定義一個方法:
* execute(Runnable command):執(zhí)行Ruannable類型的任務(wù)
* (2)ExecutorService:繼承了Executor的方法,并提供了執(zhí)行Callable任務(wù)和中止任務(wù)執(zhí)行的服務(wù),
* 其定義的方法主要有:
* submit(task):可用來提交Callable或Runnable任務(wù),并返回代表此任務(wù)的Future對象
* invokeAll(collection of tasks):批處理任務(wù)集合,并返回一個代表這些任務(wù)的Future對象集合
* shutdown():在完成已提交的任務(wù)后關(guān)閉服務(wù),不再接受新任務(wù)
* shutdownNow():停止所有正在執(zhí)行的任務(wù)并關(guān)閉服務(wù)。
* isTerminated():測試是否所有任務(wù)都執(zhí)行完畢了。
* isShutdown():測試是否該ExecutorService已被關(guān)閉
* (3)ScheduledExecutorService:繼承ExecutorService,提供了按時間安排執(zhí)行任務(wù)的功能、
* schedule(task, initDelay): 安排所提交的Callable或Runnable任務(wù)在initDelay指定的時間后執(zhí)行。
* scheduleAtFixedRate():安排所提交的Runnable任務(wù)按指定的間隔重復(fù)執(zhí)行
* scheduleWithFixedDelay():安排所提交的Runnable任務(wù)在每次執(zhí)行完后,等待delay所指定的時間后重復(fù)執(zhí)行。
*
* 通過Executors類來獲得各種服務(wù)對象。
* callable(Runnable task): 將Runnable的任務(wù)轉(zhuǎn)化成Callable的任務(wù)
* newSingleThreadExecutor: 產(chǎn)生一個ExecutorService對象,這個對象只有一個線程可用來執(zhí)行任務(wù),若任務(wù)多于一個,任務(wù)將按先后順序執(zhí)行。
* newCachedThreadPool(): 產(chǎn)生一個ExecutorService對象,這個對象帶有一個線程池,線程池的大小會根據(jù)需要調(diào)整,線程執(zhí)行完任務(wù)后返回線程池,供執(zhí)行下一次任務(wù)使用。
* newFixedThreadPool(int poolSize):產(chǎn)生一個ExecutorService對象,這個對象帶有一個大小為poolSize的線程池,若任務(wù)數(shù)量大于poolSize,任務(wù)會被放在一個queue里順序執(zhí)行。
* newSingleThreadScheduledExecutor:產(chǎn)生一個ScheduledExecutorService對象,這個對象的線程池大小為1,若任務(wù)多于一個,任務(wù)將按先后順序執(zhí)行。
* newScheduledThreadPool(int poolSize): 產(chǎn)生一個ScheduledExecutorService對象,這個對象的線程池大小為poolSize,若任務(wù)數(shù)量大于poolSize,任務(wù)會在一個queue里等待執(zhí)行
*/
public
class
ExecuteArch
{
/** */
/**
* 該線程輸出一行字符串
*/
public
static
class
MyThread
implements
Runnable
{
public
void
run()
{
System.out.println(
"
Task repeating.
"
+
System.currentTimeMillis());
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e)
{
System.out.println(
"
Task interrupted.
"
+
System.currentTimeMillis());
}
}
}
/** */
/**
* 該Callable結(jié)束另一個任務(wù)
*/
public
static
class
MyCallable
implements
Callable
{
private
Future future;
public
MyCallable(Future future)
{
this
.future
=
future;
}
public
String call()
{
System.out.println(
"
To cancell Task
"
+
+
System.currentTimeMillis());
this
.future.cancel(
true
);
return
"
Task cancelled!
"
;
}
}
/** */
/**
*
@param
args
*
@throws
ExecutionException
*
@throws
InterruptedException
*/
public
static
void
main(String[] args)
throws
InterruptedException,
ExecutionException
{
//
產(chǎn)生一個ExecutorService對象,這個對象帶有一個線程池,線程池的大小會根據(jù)需要調(diào)整,
//
線程執(zhí)行完任務(wù)后返回線程池,供執(zhí)行下一次任務(wù)使用。
ExecutorService cachedService
=
Executors.newCachedThreadPool();
Future myThreadFuture
=
cachedService.submit(
new
MyThread());
Future myCallableFuture
=
cachedService.submit(
new
MyCallable(
myThreadFuture));
System.out.println(myCallableFuture.get());
System.out.println(
"
-----------------
"
);
//
將Runnable任務(wù)轉(zhuǎn)換成Callable任務(wù)
Callable myThreadCallable
=
Executors.callable(
new
MyThread());
Future myThreadCallableFuture
=
cachedService.submit(myThreadCallable);
//
對于Runnable任務(wù),轉(zhuǎn)換成Callable任務(wù)后,也沒有返回值
System.out.println(myThreadCallableFuture.get());
cachedService.shutdownNow();
System.out.println(
"
-----------------
"
);
//
產(chǎn)生一個ExecutorService對象,這個對象帶有一個大小為poolSize的線程池,
//
若任務(wù)數(shù)量大于poolSize,任務(wù)會被放在一個queue里順序執(zhí)行
ExecutorService fixedService
=
Executors.newFixedThreadPool(
2
);
fixedService.submit(
new
MyThread());
fixedService.submit(
new
MyThread());
//
由于線程池大小為2,所以后面的任務(wù)必須等待前面的任務(wù)執(zhí)行完后才能被執(zhí)行。
myThreadFuture
=
fixedService.submit(
new
MyThread());
myCallableFuture
=
fixedService.submit(
new
MyCallable(myThreadFuture));
System.out.println(myCallableFuture.get());
fixedService.shutdownNow();
System.out.println(
"
-----------------
"
);
//
產(chǎn)生一個ScheduledExecutorService對象,這個對象的線程池大小為poolSize,
//
若任務(wù)數(shù)量大于poolSize,任務(wù)會在一個queue里等待執(zhí)行
ScheduledExecutorService fixedScheduledService
=
Executors
.newScheduledThreadPool(
2
);
//
新建任務(wù)1
MyThread task1
=
new
MyThread();
//
使用任務(wù)執(zhí)行服務(wù)立即執(zhí)行任務(wù)1,而且此后每隔2秒執(zhí)行一次任務(wù)1。
myThreadFuture
=
fixedScheduledService.scheduleAtFixedRate(task1,
0
,
2
,
TimeUnit.SECONDS);
//
新建任務(wù)2
MyCallable task2
=
new
MyCallable(myThreadFuture);
//
使用任務(wù)執(zhí)行服務(wù)等待5秒后執(zhí)行任務(wù)2,執(zhí)行它后會將任務(wù)1關(guān)閉。
myCallableFuture
=
fixedScheduledService.schedule(task2,
5
,
TimeUnit.SECONDS);
System.out.println(myCallableFuture.get());
fixedScheduledService.shutdownNow();
}
}
--
學(xué)海無涯
Powered by:
BlogJava
Copyright © 啥都寫點(diǎn)
主站蜘蛛池模板:
亚洲国语精品自产拍在线观看
|
色费女人18女人毛片免费视频
|
亚洲美女高清一区二区三区
|
www视频在线观看免费
|
久久嫩草影院免费看夜色
|
亚洲欧美成aⅴ人在线观看
|
亚洲色图在线观看
|
亚洲精品乱码久久久久久中文字幕
|
狼友av永久网站免费观看
|
真人做A免费观看
|
日本免费中文视频
|
中文字幕乱码系列免费
|
美女裸体无遮挡免费视频网站
|
亚洲福利一区二区
|
亚洲一区二区成人
|
亚洲国产成人一区二区三区
|
亚洲性在线看高清h片
|
国产jizzjizz视频全部免费
|
在线a毛片免费视频观看
|
久久久久久久免费视频
|
**毛片免费观看久久精品
|
久久久精品免费视频
|
a毛片在线看片免费
|
成人av片无码免费天天看
|
jyzzjyzz国产免费观看
|
猫咪www免费人成网站
|
国产精品亚洲精品爽爽
|
亚洲成av人无码亚洲成av人
|
亚洲欧美日韩综合久久久久
|
jiz zz在亚洲
|
国产 亚洲 中文在线 字幕
|
亚洲欧洲av综合色无码
|
亚洲av纯肉无码精品动漫
|
亚洲AV成人精品日韩一区
|
亚洲成aⅴ人片久青草影院按摩
|
亚洲午夜福利在线视频
|
亚洲成a人片在线不卡一二三区
|
亚洲乱码中文字幕在线
|
国产精品亚洲综合久久
|
亚洲av日韩av永久无码电影
|
香蕉视频免费在线播放
|