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