色欲av一区久久精品_久久综合色综合色88_无码在线观看不卡_色黄视频网站_亚洲国产精品久久久久秋霞66

java的線程池的使用

時間:2023-05-17

1、線程池的創(chuàng)立
1、首先創(chuàng)立一個類,然后完成Runnable接口
publicclassExectorTestimplementsRunnable{}
2、首先聲明一個線程池的全局變量
publicclassExectorTestimplementsRunnable{
//線程池
privateExecutorServiceexecutorPool;
}Java
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!!”);
}
}
}
}

文章標簽:

Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權利。 粵ICP備09033321號

與項目經理交流
掃描二維碼
與項目經理交流
掃描二維碼
與項目經理交流
ciya68