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; } } }