Fresco Fbcore源码分析_executor(一)

本文深入探讨Fresco的Fbcore组件,重点分析Executor的三大实现:CallerThreadExecutor、ConstrainedExecutorService和DefaultSerialExecutorService。通过类结构、方法解析及流程图,揭示Executor如何在Fresco中协调任务执行。
摘要由CSDN通过智能技术生成

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);
    } 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值