本文是抄的.
文章出處:http://gityuan.com/2016/01/16/thread-pool/
一.場景
需要用到線程池的不同場景.
二.線程池的不同用法
利用Executors
類提供了4種不同的線程池:newCachedThreadPool
, newFixedThreadPool
, newScheduledThreadPool
, newSingleThreadExecutor
1.newCachedThreadPool
創(chuàng)建一個(gè)可緩存的無界線程池栋艳,該方法無參數(shù)。當(dāng)線程池中的線程空閑時(shí)間超過60s則會自動回收該線程弹囚,當(dāng)任務(wù)超過線程池的線程數(shù)則創(chuàng)建新線程屹篓。線程池的大小上限為Integer.MAX_VALUE棍潘,可看做是無限大筋岛。
public void cachedThreadPoolDemo(){
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
final int index = i;
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", index="+index);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
結(jié)果
pool-1-thread-1, index=0
pool-1-thread-1, index=1
pool-1-thread-1, index=2
pool-1-thread-1, index=3
pool-1-thread-1, index=4
2.newFixedThreadPool
創(chuàng)建一個(gè)固定大小的線程池,該方法可指定線程池的固定大小峭火,對于超出的線程會在LinkedBlockingQueue隊(duì)列中等待爱致。
public void fixedThreadPoolDemo(){
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 6; i++) {
final int index = i;
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", index="+index);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:
pool-1-thread-1, index=0
pool-1-thread-2, index=1
pool-1-thread-3, index=2
pool-1-thread-1, index=3
pool-1-thread-2, index=4
pool-1-thread-3, index=5
3. newSingleThreadExecutor
創(chuàng)建一個(gè)只有線程的線程池谨胞,該方法無參數(shù),所有任務(wù)都保存隊(duì)列LinkedBlockingQueue中蒜鸡,等待唯一的單線程來執(zhí)行任務(wù)胯努,并保證所有任務(wù)按照指定順序(FIFO或優(yōu)先級)執(zhí)行。
public void singleThreadExecutorDemo(){
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 3; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", index="+index);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:
pool-1-thread-1, index=0
pool-1-thread-1, index=1
pool-1-thread-1, index=2
從運(yùn)行結(jié)果可以看出逢防,所有任務(wù)都是在單一線程運(yùn)行的叶沛。
4. newScheduledThreadPool
創(chuàng)建一個(gè)可定時(shí)執(zhí)行或周期執(zhí)行任務(wù)的線程池,該方法可指定線程池的核心線程個(gè)數(shù)忘朝。
public void scheduledThreadPoolDemo(){
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
//定時(shí)執(zhí)行一次的任務(wù)灰署,延遲1s后執(zhí)行
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", delay 1s");
}
}, 1, TimeUnit.SECONDS);
//周期性地執(zhí)行任務(wù),延遲2s后局嘁,每3s一次地周期性執(zhí)行任務(wù)
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", every 3s");
}
}, 2, 3, TimeUnit.SECONDS);
}
運(yùn)行結(jié)果:
pool-1-thread-1, delay 1s
pool-1-thread-1, every 3s
pool-1-thread-2, every 3s
pool-1-thread-2, every 3s
...
- schedule(Runnable command, long delay, TimeUnit unit)溉箕,延遲一定時(shí)間后執(zhí)行Runnable任務(wù);
- schedule(Callable callable, long delay, TimeUnit unit)悦昵,延遲一定時(shí)間后執(zhí)行Callable任務(wù)肴茄;
- scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit),延遲一定時(shí)間后但指,以間隔period時(shí)間的頻率周期性地執(zhí)行任務(wù)寡痰;
- scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit),與scheduleAtFixedRate()方法很類似棋凳,但是不同的是scheduleWithFixedDelay()方法的周期時(shí)間間隔是以上一個(gè)任務(wù)執(zhí)行結(jié)束到下一個(gè)任務(wù)開始執(zhí)行的間隔拦坠,而scheduleAtFixedRate()方法的周期時(shí)間間隔是以上一個(gè)任務(wù)開始執(zhí)行到下一個(gè)任務(wù)開始執(zhí)行的間隔,也就是這一些任務(wù)系列的觸發(fā)時(shí)間都是可預(yù)知的剩岳。
ScheduledExecutorService功能強(qiáng)大贞滨,對于定時(shí)執(zhí)行的任務(wù),建議多采用該方法拍棕。