Java通過Executors提供四種線程池挟秤,分別為:
newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過處理需要,可靈活回收空閑線程追迟,若無可回收,則新建線程骚腥。
newFixedThreadPool創(chuàng)建一個(gè)定長(zhǎng)線程池敦间,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待束铭。
newScheduledThreadPool創(chuàng)建一個(gè)定長(zhǎng)線程池廓块,支持定時(shí)及周期性任務(wù)執(zhí)行。
newSingleThreadExecutor創(chuàng)建一個(gè)單線程化的線程池契沫,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù)带猴,保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行。
(1)可緩存線程池 CachedThreadPool
ExecuterService exec = Executers.newCachedThreadPool(new CustomThreadFactory());
(2)定長(zhǎng)線程池 用的最多的線程池 FixedThreadPool
ExecuterService exec = Executers.newFixedThreadPool(3,new CustomThreadFactory()); //thread Num
(3)可調(diào)度線程池 ?ScheduledTreadPool
ScheduledExecuterService exec = Executers.newScheduledThreadPool(3,new CustomFactory());
(4)單線程化線程池 SingleThreadExecutor
ExecuterService exec = Executer.newSingleThreadExecutor(new CustomFactory());
測(cè)試類如下:
CustomTask.class:
public?class?CustomTask?implements?Runnable{
private?int?id= 0;
public?CustomTask(intid){
this.id= id;
}
@Override
publicvoidrun() {
//TODOAuto-generated method stub
System.out.println("時(shí)間:"+System.currentTimeMillis()+"? id="+id);
try{
Thread.sleep(1000);
}catch(InterruptedException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
}
CustomThreadFactory.class
public?class?CustomThreadFactory?implementsThreadFactory{
@Override
publicThread newThread(Runnable r) {
//TODOAuto-generated method stub
Thread t =newThread(r);
returnt;
}
}
TestMain.class
publicclassTesThread {
/**
*@paramargs
*/
publicstaticvoidmain(String[] args) {
//TODOAuto-generated method stub
//定長(zhǎng)線程池
//????????????testFixedThreadPool();
//????????????testCahedThreadPool();
//????????????testScheduledThreadPoolDelead();
//????????????testSchedulRate();
//????????????testSingleThreadPool();
}
/**
* 可緩存線程池埠褪,
*/
publicstaticvoidtestCahedThreadPool(){
ExecutorService exec = Executors.newCachedThreadPool(newCustomThreadFactory());
for(inti=0;i<10;i++){
exec.submit(newCustomTask(i));
}
exec.shutdown();
}
/**
* 定長(zhǎng)線程池浓利,超過數(shù)目挤庇,將排對(duì)等待
*
*/
public?static?void?testFixedThreadPool(){
ExecutorService exec = Executors.newFixedThreadPool(1,newCustomThreadFactory());
for(inti=0;i<5;i++){
exec.submit(newCustomTask(i));
}
exec.shutdown();
}
/**
* 調(diào)度線程池之延遲執(zhí)行線程池
*/
public?static?void?testScheduledThreadPoolDelead(){
ScheduledExecutorService exec = Executors.newScheduledThreadPool(3,newCustomThreadFactory());
for(inti=0;i<5;i++){
exec.schedule(newCustomTask(i), 3,TimeUnit.SECONDS);
}
//????????????exec.shutdown();
}
/**
* 循環(huán)調(diào)度線程
*/
public?static?void?testSchedulRate(){
Scheduled ExecutorService exec = Executors.newScheduledThreadPool(3,newCustomThreadFactory());
for(inti=0;i<5;i++){
exec.scheduleAtFixedRate(newCustomTask(i), 1, 5, TimeUnit.SECONDS);
}
exec.shutdown();
}
/**
* 單線程,線程池
*/
public?static?void?testSingleThreadPool(){
ExecutorService exec = Executors.newSingleThreadExecutor(newCustomThreadFactory());
for(inti=0;i<5;i++){
exec.submit(newCustomTask(i));
}
exec.shutdown();
}
}