每天一例多執行緒[day17]-----Executors框架
-
/**
-
* 以下四種底層實現核心都是例項化了一個ThreadPoolExecutor物件返回,其實就是根據以下四種傳遞的具體引數
-
* 不同而實現的四種不同的功能。
-
*
-
* ThreadPoolExecutor來實現以下四種的原因是,當以下四種不滿足我們需求時,我們可以
-
* 自定義執行緒池。
-
* 構造引數:
-
* public ThreadPoolExecutor(
-
* int corePoolSize,--當前執行緒池核心執行緒數
-
* int maximumPoolSize,--當前執行緒池最大執行緒數
-
* long keepAliveTime,--保持活著的空間時間
-
* TimeUnit unit,--時間單位
-
* BlockingQueue<Runnable> workQueue,--排隊等待的自定義佇列
-
* ThreadFactoty threadFactory,
-
* RejectedExecutionHandler handler--佇列滿以後,其他任務被拒絕執行的方法
-
* ){.........}
-
*
-
*/
newFiexedThreadPool
該方法返回一個固定數量的執行緒池,該方法的執行緒數量始終不變,當有一個任務提交時,若執行緒池中空閒,則立即執行,若沒有則會被暫緩在一個任務佇列中等待有空閒的執行緒去執行。
-
/*
-
* public static ExecutorService newFixedThreadPool(int nThreads) {
-
return new ThreadPoolExecutor(nThreads, nThreads,
-
0L, TimeUnit.MILLISECONDS,
-
new LinkedBlockingQueue<Runnable>());
-
}
-
* 核心執行緒數=最大執行緒數,空閒時間0s,執行緒執行完直接回收,但是維持執行緒池個數固定,
-
* 沒有空閒執行緒的話,執行緒會被快取在LinkedBlockingQueue無界阻塞佇列中
-
*/
-
// ExecutorService pool = Executors.newFixedThreadPool(10);
newSingleThreadExecutor
建立有一個執行緒的執行緒池,若空閒則執行,若沒有空閒則暫緩在任務佇列中。
-
/*
-
* 和固定差不多,僅有1個執行緒
-
* public static ExecutorService newSingleThreadExecutor() {
-
return new FinalizableDelegatedExecutorService
-
(new ThreadPoolExecutor(1, 1,
-
0L, TimeUnit.MILLISECONDS,
-
new LinkedBlockingQueue<Runnable>()));
-
}
-
*/
-
// ExecutorService pool = Executors.newSingleThreadExecutor();
newCachedThreadPool
返回一個可以根據實際情況調整執行緒個數的執行緒池,不限制最大執行緒數量,若有空閒的執行緒則執行任務,若無任務則不建立執行緒,並且空閒執行緒會在60s後自動回收。
-
/**
-
* 初始化時核心執行緒0,不限制執行緒最大數,
-
* 只要任務來了就建立一個執行緒並且執行任務,使用SynchronousQueue,沒有任何容量。
-
* 執行緒最大空閒時間60s,如果執行緒在60s內沒有任務到來就回收執行緒。如果59s的時候來了,就去執行
-
* 任務不會回收執行緒。
-
*
-
* public static ExecutorService newCachedThreadPool() {
-
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
-
60L, TimeUnit.SECONDS,
-
new SynchronousQueue<Runnable>());
-
}
-
*/
-
// ExecutorService pool = Executors.newCachedThreadPool();
newScheduledThreadPool
該方法返回一個ScheduledExecutorService物件,但該執行緒池可以指定執行緒的數量。
-
/**
-
* 初始化指定的核心執行緒數,且沒有最大執行緒數限制,空閒時間0s,執行完任務就回收執行緒,
-
* 執行緒沒有空閒時任務到來,放到DelayedWorkQueue中。只有等延遲時間到了,任務才能被取出執行。
-
*
-
*
-
* public ScheduledThreadPoolExecutor(int corePoolSize) {
-
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
-
new DelayedWorkQueue());
-
}
-
*/
-
// ExecutorService pool = Executors.newScheduledThreadPool(10);
舉例:
-
import java.util.concurrent.Executors;
-
import java.util.concurrent.ScheduledExecutorService;
-
import java.util.concurrent.ScheduledFuture;
-
import java.util.concurrent.TimeUnit;
-
class Temp extends Thread {
-
public void run() {
-
System.out.println("run");
-
}
-
}
-
public class ScheduledJob {
-
public static void main(String args[]) throws Exception {
-
Temp command = new Temp();
-
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
-
/**
-
* scheduleWithFixedDelay(
-
* Runnable command, 具體執行緒任務
-
* long initialDelay, 初始化延遲執行時間
-
* long delay, 輪詢執行時間,每隔delay時間執行一次
-
* TimeUnit unit 單位
-
* )
-
*
-
*/
-
//5s後,每隔1s執行一次command
-
ScheduledFuture<?> scheduleTask =
-
scheduler.scheduleWithFixedDelay(command, 5, 1, TimeUnit.SECONDS);
-
}
-
}
列印:
-
run
-
run
-
run
-
run