以上就是给各位分享Executors.newFixedThreadPool,其中也会对1和Executors.newSingleThreadExecutor进行解释,同时本文还将给你拓展com.bum
以上就是给各位分享Executors.newFixedThreadPool,其中也会对1和Executors.newSingleThreadExecutor进行解释,同时本文还将给你拓展com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor的实例源码、Executors.newCachedThreadPool() 与 Executors.newFixedThreadPool()、Executors.newFixedThreadPool (5); 这种线程的大小应该怎么设置,和电脑配置计算?、Executors.newFixedThreadPool 源码解析等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- Executors.newFixedThreadPool(1)和Executors.newSingleThreadExecutor()之间的区别(threadpoolexcutor和excutor关系)
- com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor的实例源码
- Executors.newCachedThreadPool() 与 Executors.newFixedThreadPool()
- Executors.newFixedThreadPool (5); 这种线程的大小应该怎么设置,和电脑配置计算?
- Executors.newFixedThreadPool 源码解析
Executors.newFixedThreadPool(1)和Executors.newSingleThreadExecutor()之间的区别(threadpoolexcutor和excutor关系)
我的问题是:使用有意义吗Executors.newFixedThreadPool(1)??
?在两个线程(main +
oneAnotherThread)中,使用执行程序服务效率高吗?是否通过调用new Runnable(){}
比使用ExecutorService更好地直接创建新线程?在这种情况下使用ExecutorService有什么好处和坏处?
PS:主线程和oneAnotherThread不访问任何公共资源。
我经历了:使用ExecutorService有什么优势?。一次只能有一个线程!
答案1
小编典典使用有意义
Executors.newFixedThreadPool(1)
吗?
它与a本质上是相同的,Executors.newSingleThreadExecutor()
不同之处在于后者是不可重新配置的(如javadoc所示),而前者是将其转换为ThreadPoolExecutor
。
在两个线程(main + oneAnotherThread)中,使用执行程序服务效率高吗?
执行程序服务是围绕线程的非常薄的包装器,可显着促进线程生命周期管理。如果您只需要newThread(runnable).start();
继续前进,那么就不需要ExecutorService了。
在任何现实生活中,都有可能监视任务的生命周期(通过返回的Future
s),执行者将在未捕获的异常情况下根据需要重新创建线程的事实,回收线程与创建新的解决方案等,使执行者服务成为功能更强大的解决方案,而无需支付额外的费用。
底线:使用执行程序服务和线程相比,我看不到任何不利之处。
Executors.newSingleThreadExecutor()。execute(command)和new
Thread(command).start();之间的区别
经历了两个选项之间行为上的细微差异。
com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor的实例源码
@Override public void applyOptions(Context context,GlideBuilder builder) { //builder.setMemoryCache(new LruResourceCache(64 * 1024 * 1024)); //builder.setBitmapPool(new LruBitmapPool(32 * 1024 * 1024)); builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); builder.setdiskCache(new InternalCachediskCacheFactory(context,2147483647)); builder.setResizeService(new FifoPriorityThreadPoolExecutor(2)); }
@Override public void applyOptions(Context context,2147483647)); builder.setResizeService(new FifoPriorityThreadPoolExecutor(2)); }
@Override public void applyOptions(Context context,GlideBuilder builder) { //if (VERSION.SDK_INT == VERSION_CODES.KITKAT) builder.setResizeService(new FifoPriorityThreadPoolExecutor(4)); }
@Override public void applyOptions(Context context,GlideBuilder builder) { // debug: make sure there's enough threads so delay transformations don't block the others builder.setdiskCacheService(new FifoPriorityThreadPoolExecutor(20)); builder.setResizeService(new FifoPriorityThreadPoolExecutor(20)); }
Executors.newCachedThreadPool() 与 Executors.newFixedThreadPool()
newCachedThreadPool()
相对newFixedThreadPool()
我什么时候应该使用其中一种?在资源利用方面哪种策略更好?
Executors.newFixedThreadPool (5); 这种线程的大小应该怎么设置,和电脑配置计算?
ExecutorService pool=Executors.newFixedThreadPool (5);// 创建一个固定大小为 5 的线程池
这种线程的大小应该怎么设置,和电脑配置计算?
for(int i=0;i<7;i++){
pool.submit(new MyThread());
}
pool.shutdown();
Executors.newFixedThreadPool 源码解析
Executors 有个常用静态方法 newFixedThreadPool (int nThreads),来构造线程池 今天我们其源码实现,探一探究竟
//new LinkedBlockingQueue<Runnable>()这里可以看出 是声明的无界队列大小,默认大小为Integer.MAX_VALUE
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
点进去看其实现 corePoolSize、maximumPoolSize 两个值设置为一样,keepAliveTime 为空闲线程存活时间,when the number of threads is greater than the core, 这里该值为 0
//Executors.defaultThreadFactory() 用来构造线程的 Returns a default thread factory used to create new threads.
//defaultHandler: the default rejected execution handler,
//A handler for rejected tasks that throws a RejectedExecutionException 当 Runnable task处理不过来时会派上用场
//private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
再看下一步调用
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
至此调用完成。
总结:
-
newFixedThreadPool 提供一种快速构造线程池的接口,里面设置了很多默认参数,最终还是调用 ThreadPoolExecutor 来构造 ExecutorService,ThreadPoolExecutor 本身是 ExecutorService 接口类的实现类。
-
当默认参数不满足需要是,直接 使用 ThreadPoolExecutor 进行构造线程池
今天关于Executors.newFixedThreadPool和1和Executors.newSingleThreadExecutor的分享就到这里,希望大家有所收获,若想了解更多关于com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor的实例源码、Executors.newCachedThreadPool() 与 Executors.newFixedThreadPool()、Executors.newFixedThreadPool (5); 这种线程的大小应该怎么设置,和电脑配置计算?、Executors.newFixedThreadPool 源码解析等相关知识,可以在本站进行查询。
本文标签: