public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
public static ExecutorService newFixedThreadPool(int nThreads){ returnnewThreadPoolExecutor(nThreads, nThreads, 0L,TimeUnit.MILLISECONDS, newLinkedBlockingQueue<Runnable>()); }
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory){ returnnewFinalizableDelegatedExecutorService (newThreadPoolExecutor(1,1, 0L,TimeUnit.MILLISECONDS, newLinkedBlockingQueue<Runnable>(), threadFactory)); }
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize){ returnnewScheduledThreadPoolExecutor(corePoolSize); }
corePoolSize
:核心線程數,默認狀況下,核心線程會在線程中一直存活;
maximumPoolSize
:最大線程數,當活動線程數達到這個數值後,後續的任務將會被阻塞;
keepAliveTime
:非核心線程閒置時的超時時長,超過這個時長,閒置的非核心線程就會被回收;
unit
:用於指定keepAliveTime參數的時間單位,有
TimeUnit.MILLISECONDS
、
TimeUnit.SECONDS
、
TimeUnit.MINUTES
等;
workQueue
:任務隊列,經過線程池的execute方法提交的Runnable對象會存儲在這個參數中;
threadFactory
:線程工廠,爲線程池提供建立新線程的功能。它是一個接口,它只有一個方法
Thread newThread(Runnable r)
;
RejectedExecutionHandler
:當線程池沒法執行新任務時,多是因爲任務隊列已滿或者是沒法成功執行任務,這個時候就會調用這個
Handler的
rejectedExecution
方法來通知調用者,默認狀況下,
rejectedExecution
會直接拋出個
rejectedExecutionException
。
RejectedExecutionHandler
的
rejectedExecution
方法來通知調用者。
public final class ThreadUtils { private static final String TAG = ThreadUtils.class.getSimpleName(); //線程池爲無限大,複用線程,靈活回收空閒線程 // name:線程名字 public static ThreadPoolExecutor newCachedThreadPool(final String name) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new CounterThreadFactory(name), new LogDiscardPolicy()); } //定長線程池,可控制線程最大併發數,超出的線程會在隊列中等待 //name:線程名字, nThread:線程數 public static ThreadPoolExecutor newFixedThreadPool(final String name, int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new CounterThreadFactory(name), new LogDiscardPolicy()); } //建立一個單線程化的線程池,它只會用惟一的工做線程來執行任務,保證全部任務按照指定順序(FIFO, LIFO, 優先級)執行 //name:線程名字 public static ThreadPoolExecutor newSingleThreadExecutor(final String name) { return newFixedThreadPool(name, 1); } //建立一個定長線程池,支持定時及週期性任務執行。 /*使用: scheduledThreadPool.schedule(new Runnable() { @Override public void run() { System.out.println("delay 3 seconds"); } }, 3, TimeUnit.SECONDS); //表示延遲3秒執行。 */ public static ScheduledExecutorService newScheduledExecutorService(int nThreads){ return Executors.newScheduledThreadPool(nThreads); } public static class LogDiscardPolicy implements RejectedExecutionHandler { public LogDiscardPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { LogUtils.v(TAG, "rejectedExecution() " + r + " is discard."); } } public static class CounterThreadFactory implements ThreadFactory { private int count; private String name; public CounterThreadFactory(String name) { this.name = (name == null ? "Android" : name); } @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setName(name + "-thread #" + count++); return thread; } } }