Java通過(guò)Executors提供四種線程池春缕,分別為:
newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過(guò)處理需要,可靈活回收空閑線程混槐,若無(wú)可回收戈锻,則新建線程歼跟。
newFixedThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線程池,可控制線程最大并發(fā)數(shù)格遭,超出的線程會(huì)在隊(duì)列中等待哈街。
newScheduledThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線程池,支持定時(shí)及周期性任務(wù)執(zhí)行拒迅。
newSingleThreadExecutor 創(chuàng)建一個(gè)單線程化的線程池拴驮,它只會(huì)用唯一的工作線程來(lái)執(zhí)行任務(wù)养泡,保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行塘幅。
(1) newCachedThreadPool
創(chuàng)建一個(gè)可緩存線程池蛾派,如果線程池長(zhǎng)度超過(guò)處理需要,可靈活回收空閑線程往毡,若無(wú)可回收蒙揣,則新建線程。示例代碼如下:
packagetest;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
publicclassThreadPoolExecutorTest?{
publicstaticvoidmain(String[]?args)?{
ExecutorService?cachedThreadPool?=?Executors.newCachedThreadPool();
for(inti?=0;?i?<10;?i++)?{
finalintindex?=?i;
try{
Thread.sleep(index?*1000);
}catch(InterruptedException?e)?{
e.printStackTrace();
}
cachedThreadPool.execute(newRunnable()?{
publicvoidrun()?{
System.out.println(index);
}
});
}
}
}
線程池為無(wú)限大开瞭,當(dāng)執(zhí)行第二個(gè)任務(wù)時(shí)第一個(gè)任務(wù)已經(jīng)完成懒震,會(huì)復(fù)用執(zhí)行第一個(gè)任務(wù)的線程罩息,而不用每次新建線程。
(2) newFixedThreadPool
創(chuàng)建一個(gè)定長(zhǎng)線程池个扰,可控制線程最大并發(fā)數(shù)瓷炮,超出的線程會(huì)在隊(duì)列中等待。示例代碼如下:
packagetest;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
publicclassThreadPoolExecutorTest?{
publicstaticvoidmain(String[]?args)?{
ExecutorService?fixedThreadPool?=?Executors.newFixedThreadPool(3);
for(inti?=0;?i?<10;?i++)?{
finalintindex?=?i;
fixedThreadPool.execute(newRunnable()?{
publicvoidrun()?{
try{
System.out.println(index);
Thread.sleep(2000);
}catch(InterruptedException?e)?{
e.printStackTrace();
}
}
});
}
}
}
因?yàn)榫€程池大小為3递宅,每個(gè)任務(wù)輸出index后sleep 2秒娘香,所以每?jī)擅氪蛴?個(gè)數(shù)字。
定長(zhǎng)線程池的大小最好根據(jù)系統(tǒng)資源進(jìn)行設(shè)置办龄。如Runtime.getRuntime().availableProcessors()
(3)? newScheduledThreadPool
創(chuàng)建一個(gè)定長(zhǎng)線程池烘绽,支持定時(shí)及周期性任務(wù)執(zhí)行。延遲執(zhí)行示例代碼如下:
packagetest;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ScheduledExecutorService;
importjava.util.concurrent.TimeUnit;
publicclassThreadPoolExecutorTest?{
publicstaticvoidmain(String[]?args)?{
ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);
scheduledThreadPool.schedule(newRunnable()?{
publicvoidrun()?{
System.out.println("delay?3?seconds");
}
},3,?TimeUnit.SECONDS);
}
}
表示延遲3秒執(zhí)行俐填。
定期執(zhí)行示例代碼如下:
packagetest;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ScheduledExecutorService;
importjava.util.concurrent.TimeUnit;
publicclassThreadPoolExecutorTest?{
publicstaticvoidmain(String[]?args)?{
ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);
scheduledThreadPool.scheduleAtFixedRate(newRunnable()?{
publicvoidrun()?{
System.out.println("delay?1?seconds,?and?excute?every?3?seconds");
}
},1,3,?TimeUnit.SECONDS);
}
}
表示延遲1秒后每3秒執(zhí)行一次安接。
(4) newSingleThreadExecutor
創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來(lái)執(zhí)行任務(wù)英融,保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行盏檐。示例代碼如下:
packagetest;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
publicclassThreadPoolExecutorTest?{
publicstaticvoidmain(String[]?args)?{
ExecutorService?singleThreadExecutor?=?Executors.newSingleThreadExecutor();
for(inti?=0;?i?<10;?i++)?{
finalintindex?=?i;
singleThreadExecutor.execute(newRunnable()?{
publicvoidrun()?{
try{
System.out.println(index);
Thread.sleep(2000);
}catch(InterruptedException?e)?{
e.printStackTrace();
}
}
});
}
}
}
結(jié)果依次輸出,相當(dāng)于順序執(zhí)行各個(gè)任務(wù)驶悟。
你可以使用JDK自帶的監(jiān)控工具來(lái)監(jiān)控我們創(chuàng)建的線程數(shù)量胡野,運(yùn)行一個(gè)不終止的線程,創(chuàng)建指定量的線程痕鳍,來(lái)觀察:
工具目錄:C:\Program Files\Java\jdk1.6.0_06\bin\jconsole.exe
運(yùn)行程序做稍微修改:
packagetest;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
publicclassThreadPoolExecutorTest?{
publicstaticvoidmain(String[]?args)?{
ExecutorService?singleThreadExecutor?=?Executors.newCachedThreadPool();
for(inti?=0;?i?<100;?i++)?{
finalintindex?=?i;
singleThreadExecutor.execute(newRunnable()?{
publicvoidrun()?{
try{
while(true)?{
System.out.println(index);
Thread.sleep(10*1000);
}
}catch(InterruptedException?e)?{
e.printStackTrace();
}
}
});
try{
Thread.sleep(500);
}catch(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
效果如下:
選擇我們運(yùn)行的程序:
監(jiān)控運(yùn)行狀態(tài)