Fbcore简介
Fbcore包含两大模块,一个是common 另一个是datasource ,它主要提供了Fresco 框架通用的接口和相关的API。这些API被Imagepipeline 和 drawee大量的使用
Executor功能分析
Executor类简介
1.CallerThreadExecutor.java
CallerThreadExecutor是一个单例类,它将线程的执行过程进行了封装,所有的线程的运行都可以通过下面的方法来运行,简单来说他是一个线程的执行者
public void execute(Runnable command) {
command.run();
}
2.ConstrainedExecutorService.java
ConstrainedExecutorService主要是用来处理多线程的并发操作,它同时也支持自定义并发线程的个数,接下来上代码
构造方法分析
public ConstrainedExecutorService(
String name,
int maxConcurrency,
Executor executor,
BlockingQueue<Runnable> workQueue) {
//这里使用BlockingQueue是为了保证线程不会被阻塞,它基于生产者消费者模型来实现,当队列里线程满的时候他会阻止线程的进入,当队列空的时候,他会阻止从队列里面取出线程
if (maxConcurrency <= 0) {
throw new IllegalArgumentException("max concurrency must be > 0");
}
mName = name;
mExecutor = executor;
mMaxConcurrency = maxConcurrency;//代表最大同时运行线程的个数
mWorkQueue = workQueue;
mTaskRunner = new Worker();//Workr是一个线程,它实现了从队列里拿出一个线程并执行它,同时会实时调整mPendingWorkers 的个数
mPendingWorkers = new AtomicInteger(0);//Pending work 代表正在从队列里出来的正在执行的线程的个数
mMaxQueueSize = new AtomicInteger(0);//代表线成队列的最大值
}
接下来看看这个类主要做了什么?
这个类主要通过下面两个方法来工作:execute 和 startWorkerIfNeeded,下面是具体的分析:
public void execute(Runnable runnable) {
if (runnable == null) {
throw new NullPointerException("runnable parameter is null");
}
if (!mWorkQueue.offer(runnable)) {
//将这个线程添加到队列里,如果失败则抛出异常
throw new RejectedExecutionException(
mName + " queue is full, size=" + mWorkQueue.size());
}
final int queueSize = mWorkQueue.size();//获取队列的大小
final int maxSize = mMaxQueueSize.get();//获取自定义的队列的最大值
if ((queueSize > maxSize) && mMaxQueueSize.compareAndSet(maxSize, queueSize)) {
FLog.v(TAG, "%s: max pending work in queue = %d", mName, queueSize);
}