Java類Executors詳解
Java類Executors
詳解【待完善】
1.類簡介
* Factory and utility methods for {@link Executor}, {@link
* ExecutorService}, {@link ScheduledExecutorService}, {@link
* ThreadFactory}, and {@link Callable} classes defined in this
* package. This class supports the following kinds of methods:
針對定義在這個包中的 Executor,ExecutorService,ScheduledExecutorService ,ThreadFactory,Callable
-
Methods that create and return an {@link ExecutorService} set up with commonly useful configuration settings. 使用常用且有用的配置去建立並返回一個
ExecutorService
。 -
Methods that create and return a {@link ScheduledExecutorService} set up with commonly useful configuration settings. 使用常用配置返回一個
ScheduledExecutorService
-
Methods that create and return a “wrapped” ExecutorService, that disables reconfiguration by making implementation-specific methods inaccessible. 建立並返回一個
"wrapped"
ExecutorService
,這使重配置無效,通過實現具體的,不可訪問的方法。 -
Methods that create and return a {@link ThreadFactory} that sets newly created threads to a known state. 建立並返回一個ThreadFactory,這個可以設定最近建立的執行緒到一個指定的狀態。
-
Methods that create and return a {@link Callable} out of other closure-like forms, so they can be used in execution methods requiring {@code Callable}.
2.方法介紹
newFixedThreadPool
/**
* Creates a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue. At any point, at most
* {@code nThreads} threads will be active processing tasks.
* If additional tasks are submitted when all threads are active,
* they will wait in the queue until a thread is available.
* If any thread terminates due to a failure during execution
* prior to shutdown, a new one will take its place if needed to
* execute subsequent tasks. The threads in the pool will exist
* until it is explicitly {@link ExecutorService#shutdown shutdown}.
*
* @param nThreads the number of threads in the pool
* @return the newly created thread pool
* @throws IllegalArgumentException if {@code nThreads <= 0}
*/
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
該方法的功能就是:建立一個執行緒池,可用於複用一個固定數量的執行緒,操作一個共享的很大的(任務)佇列。