1、線程池的創(chuàng)立
1、首先創(chuàng)立一個(gè)類,然后完成Runnable接口
publicclassExectorTestimplementsRunnable{}
2、首先聲明一個(gè)線程池的全局變量
publicclassExectorTestimplementsRunnable{
//線程池
privateExecutorServiceexecutorPool;
}
3、然后在run()辦法中,創(chuàng)立線程池實(shí)例,創(chuàng)立線程池的時(shí)分,切記創(chuàng)立守護(hù)線程,這樣能夠避免你的效勞中止后,效勞的線程還沒中止,就是tomcat的進(jìn)程還在的狀況呈現(xiàn)。
復(fù)制代碼
publicclassExectorTestimplementsRunnable{
//線程池
privateExecutorServiceexecutorPool;
@Override
publicvoidrun(){
//創(chuàng)立線程池,設(shè)置為守護(hù)進(jìn)程,能夠和主線程一同關(guān)閉
this.executorPool=Executors.newFixedThreadPool(this.numThreads,newThreadFactory(){
@Override
publicThreadnewThread(Runnabler){
Threadthread=Executors.defaultThreadFactory().newThread(r);
thread.setDaemon(true);
returnthread;
}
});
}
}
復(fù)制代碼
4、然后我們需求循環(huán)需求處置的辦法個(gè)數(shù),在循環(huán)中調(diào)用線程池的辦法
復(fù)制代碼
publicclassExectorTestimplementsRunnable{
//線程池
privateExecutorServiceexecutorPool;
@Override
publicvoidrun(){
//創(chuàng)立線程池,設(shè)置為守護(hù)進(jìn)程,能夠和主線程一同關(guān)閉
this.executorPool=Executors.newFixedThreadPool(this.numThreads,newThreadFactory(){
@Override
publicThreadnewThread(Runnabler){
Threadthread=Executors.defaultThreadFactory().newThread(r);
thread.setDaemon(true);
returnthread;
}
});
//循環(huán)處置的辦法
ListhandleList=newArrayList();
for(Stringhandler:handleList){
this.executorPool.submit(newHandler());
}
}
}
復(fù)制代碼
5、將處置的辦法貼上
復(fù)制代碼
publicclassExectorTestimplementsRunnable{
//線程池
privateExecutorServiceexecutorPool;
@Override
publicvoidrun(){
//創(chuàng)立線程池,設(shè)置為守護(hù)進(jìn)程,能夠和主線程一同關(guān)閉
this.executorPool=Executors.newFixedThreadPool(this.numThreads,newThreadFactory(){
@Override
publicThreadnewThread(Runnabler){
Threadthread=Executors.defaultThreadFactory().newThread(r);
thread.setDaemon(true);
returnthread;
}
});
//循環(huán)處置的辦法
ListhandleList=newArrayList();
for(Stringhandler:handleList){
this.executorPool.submit(newHandler());
}
}
/**
*數(shù)據(jù)處置線程
*/
publicstaticclassHandlerimplementsRunnable{
publicHandler(){}
@Override
publicvoidrun(){
//處置數(shù)據(jù)的辦法
}
}
}
復(fù)制代碼
2、線程池的關(guān)閉
6、最后補(bǔ)全中止線程池的辦法,@PreDestroy辦法是在spring銷毀之前會(huì)調(diào)用的辦法
復(fù)制代碼
publicclassExectorTestimplementsRunnable{
//線程池
privateExecutorServiceexecutorPool;
@Override
publicvoidrun(){
//創(chuàng)立線程池,設(shè)置為守護(hù)進(jìn)程,能夠和主線程一同關(guān)閉
this.executorPool=Executors.newFixedThreadPool(this.numThreads,newThreadFactory(){
@Override
publicThreadnewThread(Runnabler){
Threadthread=Executors.defaultThreadFactory().newThread(r);
thread.setDaemon(true);
returnthread;
}
});
//循環(huán)處置的辦法
ListhandleList=newArrayList();
for(Stringhandler:handleList){
this.executorPool.submit(newHandler());
}
}
/**
*數(shù)據(jù)處置線程
*/
publicstaticclassHandlerimplementsRunnable{
publicHandler(){}
@Override
publicvoidrun(){
//處置數(shù)據(jù)的辦法
}
}
@PreDestroy
publicvoidshutdown(){
//關(guān)閉線程池,會(huì)等候線程的執(zhí)行完成
if(this.executorPool!=null){
//關(guān)閉線程池
this.executorPool.shutdown();
//等候關(guān)閉完成,等候五秒
try{
if(!this.executorPool.awaitTermination(5,TimeUnit.SECONDS)){
log.info(“Timedoutwaitingforconsumerthreadstoshutdown,exitinguncleanly!!”);
}
}catch(InterruptedExceptione){
log.info(“Interruptedduringshutdown,exitinguncleanly!!”);
}
}
}
}
廣州天河區(qū)珠江新城富力盈力大廈北塔2706
020-38013166(網(wǎng)站咨詢專線)
400-001-5281 (售后服務(wù)熱線)
深圳市坂田十二橡樹莊園F1-7棟
Site/ http://www.szciya.com
E-mail/ itciya@vip.163.com
品牌服務(wù)專線:400-001-5281
長沙市天心區(qū)芙蓉中路三段398號(hào)新時(shí)空大廈5樓
聯(lián)系電話/ (+86 0731)88282200
品牌服務(wù)專線/ 400-966-8830
旗下運(yùn)營網(wǎng)站:
Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號(hào)