10.java Excutor框架
小于 1 分钟
固定线程池 newFixedThreadPool 缓存线程池 newCachedThreadPool 单个线程 newSingleThreadExecutor 定时线程池 newScheduledThreadPool
//固定线程池
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor( nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>() );
}
//缓存线程池
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor( 0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>() );
}
//单个线程池
public static ExecutorService newSingleThreadExecutor() {
return new ThreadPoolExecutor( 1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>() );
}
//定时线程池
public static ExecutorService newScheduledThreadPoolExecutor(int corePoolSize) {
return new ThreadPoolExecutor( corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new ScheduledThreadPoolExecutor.DelayedWorkQueue() );
}
int cpu = Runtime.getRuntime().availableProcessors();
CPU密集型:Ncpu+1
IO密集型:2* Ncpu