Volley源码分析-请求处理

本文分析了Volley网络请求的处理流程,包括请求生成、分发及返回等关键环节,并探讨了生产者-消费者模型在Volley中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

本篇分析分析Volley如何生成请求,如何处理请求,以及请求如何返回。从分析中学习再做组件时如何处理请求。

生产者-消费者模型

生产者-消费者模型

生产者-消费者模型描述的是共享的缓冲区的两类线程,一类是添加消息的生产者,一类是处理消息的消费者。而基本涉及到的多线程处理的均使用这个模型。比如Glide,比如这边文档分析的Volley,比如OkHttp,再比如JDK中的ThreadPoolExecutor。

Volley的请求分发

启动线程

根据配置RequestQueue的start方法会启动相应的线程来分发处理请求。

public void start() {
    stop();  // Make sure any currently running dispatchers are stopped.
    // Create the cache dispatcher and start it.
    mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
    mCacheDispatcher.start();

    // Create network dispatchers (and corresponding threads) up to the pool size.
    for (int i = 0; i < mDispatchers.length; i++) {
        NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
                mCache, mDelivery);
        mDispatchers[i] = networkDispatcher;
        networkDispatcher.start();
    }
}
  1. 启动一个CacheDispatcher,CacheDispatcher用于处理是否请求命中缓存直接返回。一方面CacheDispatcher是消费者,处理mCacheQueue的请求。一方面CacheDispatcher是生产者,对于未命中缓存直接添加到mNetworkQueue中。
  2. 启动NetworkDispatcher,根据配置的线程数来启动网络请求的线程。NetworkDispatcher作为mNetworkQueue的消费者,不断从mNetworkQueue获取请求进行处理。

添加网络请求

client的代码通过RequestQueue的add方法添加请求,如果设置了不保留缓存,将直接的添加到mNetworkQueue中。如果设置了,添加到mNetworkQueue中。

public <T> Request<T> add(Request<T> request) {
    // Tag the request as belonging to this queue and add it to the set of current requests.
    request.setRequestQueue(this);
    synchronized (mCurrentRequests) {
        mCurrentRequests.add(request);
    }

    // Process requests in the order they are added.
    request.setSequence(getSequenceNumber());
    request.addMarker("add-to-queue");

    // If the request is uncacheable, skip the cache queue and go straight to the network.
    if (!request.shouldCache()) {
        mNetworkQueue.add(request);
        return request;
    }
    mCacheQueue.add(request);
    return request;
 }

处理请求

缓存请求处理

在CacheDispatcher的run()方法中一直是循环处理请求。而在processRequest方法中

  1. 从缓存队列中或者请求
  2. 命中缓存直接返回
  3. 没有命中缓存直接添加到mNetworkQueue队列中。

可以自行分析processRequest方法查看逻辑。可以自行分析processRequest方法查看逻辑。

@Override
public void run() {
    while (true) {
        try {
            processRequest();
        } catch (InterruptedException e) {
            // We may have been interrupted because it was time to quit.
            if (mQuit) {
                return;
            }
        }
    }
}
网络请求处理

在NetworkDispatcher中与CacheDispatcher一样,一致循环处理请求。在mNetworkQueue中是从mNetworkQueue不断的获取请求进行网络请求。
可以自行分析processRequest方法查看逻辑。

@Override
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    while (true) {
        try {
            processRequest();
        } catch (InterruptedException e) {
            // We may have been interrupted because it was time to quit.
            if (mQuit) {
                return;
            }
        }
    }
}

总结

这篇文章只是宽泛的讲解一下,Volley的网络请求的处理流程。并没有深入的去分析,比如处理请求部分只是说明一致循环处理请求,并没有说明队列里面内容为空了,线程会怎样,队列满了如何处理请求,这块设计到线程的设计。而本篇只是从结构上说明请求的处理。后面还会针对线程进行讲解。

  1. Volley对于请求的处理采用了生产者-消费者模型。
  2. 在做组件时,需要设计如何添加请求,添加到哪里,以及如何处理请求,这块是组件的脉络,而其他的设计是骨肉涉及更细节的结构。
  3. 从请求数据处理为出发,分析第三方开源库源码更容易一些。因为基本开源库的涉及到多线程的处理(多线程处理中又以生产者-消费者模型为经典)。

Over

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值