源码下载地址:https://github.com/code-study/android-volley-analysis
一、原理
Volley有两个调度器,缓存请求调度器和网络请求调度器,两个调度器均是Thread的子类,且拥有各自的队列;初始化Volley时启动两个线程,在子线程中while(ture)循环,将请求加入队列,通过队列阻塞的方式,控制请求流程。
二、源码分析
1、使用方式
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
2、流程分析
(1)Volley.java
首先创建RequestQueue的实例;创建网络请求,再将其封装;创建缓存机制并设置缓存目录;
public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) { // context.getCacheDir() : /data/data/com.android.volley.test/cache File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
//userAgent : com.android.volley.test/1
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
//创建网络请求
if (stack == null) {
//在 Froyo(2.2) 之前,HttpURLConnection 有个重大 Bug,调用 close() 函数会影响连接池,导致连接复用失效,所以在2.2之前使用 HttpURLConnection 需要关闭 keepAlive。
// Gingerbread(2.3) HttpURLConnection 默认开启了 gzip 压缩,提高了 HTTPS 的性能,Ice Cream Sandwich(4.0) HttpURLConnection 支持了请求结果缓存
if (Build.VERSION.SDK_INT >= 9) {
//HttpURLConnection
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
//HttpClient
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
// 封装请求
Network network = new BasicNetwork(stack);
//创建请求队列
RequestQueue queue;
if (maxDiskCacheBytes <= -1) {
// No maximum size specified
queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
} else {
// Disk cache size specified
queue = new RequestQueue(new DiskBasedCache(cacheDir, maxDiskCacheBytes), network);
}
queue.start();
return queue;
}
****详细分析:** 首先设置缓存文件地址,用于缓存请求回来的结果。 创建网络请求,若android版本大于等于9的话,则使用HurlStack(实际是HttpURLConnection),否则使用HttpClientStack(实际是HttpClient),原因是在 Froyo(2.2) 之前,HttpURLConnection 有个重大 Bug,调用 close() 函数会影响连接池,导致连接复用失效,所以在2.2之前使用 HttpURLConnection 需要关闭 keepAlive。Gingerbread(2.3) HttpURLConnection 默认开启了 gzip 压缩,提高了 HTTPS 的性能,Ice Cream Sandwich(4.0) HttpURLConnection 支持了请求结果缓存。 将网络请求封装到BasicNetWork,用于真正执行请求,后面将会详细说明。 创建RequestQueue并start,可以设置最大缓存bytes(maxDiskCacheBytes),默认是5M。
(2)RequestQueue.java
Volley的核心是调度线程,首先终止所有的调度线程,即调用所有调度器的interrupt(调度器均为Thread); 启用一个缓存线程,和四个网络线程;两种调度线程run方法均开启while(true)循环,通过队列阻塞方式控制请求流程,下面会详情说明。
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.
// 网络请求调度器,默认开启DEFAULT_NETWORK_THREAD_POOL_SIZE(4)个线程,相当于线程池
for (int i = 0; i < mDispatchers.length; i++) {
NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
mDispatchers[i] = networkDispatcher;
networkDispatcher.start();
}
}
接下来调用RequestQueue的add方法,将请求加入队列;如果不需要缓存则加入网络队列,否则加入缓存队列,相应的调度线程获取队列元素解除阻塞,会往下执行相应的逻辑。加入缓存队列之前需要先判断是否有相同的请求在执行中,若有则加入等待队列,待前一次请求执行完之后,再执行。
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); //为Request设置请求队列
//将请求add到当前请求队列中
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;
}
// Insert request into stage if there's already a request with the same cache key in flight.
//缓存,首先判断是否有相同请求正在处理
synchronized (mWaitingRequests) {
String cacheKey = request.getCacheKey();
//有相同请求正在处理
if (mWaitingRequests.containsKey(cacheKey)) {
// There is already a request in flight. Queue up.
Queue<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
if (stagedRequests == null) {
stagedRequests = new LinkedList<Request<?>>();
}
stagedRequests.add(request);
//处理等待队列空数据,加入到等待队列
mWaitingRequests.put(cacheKey, stagedRequests);
if (VolleyLog.DEBUG) {
VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
}
} else {
// Insert 'null' queue for this cacheKey, indicating there is now a request in
// flight.
// 创建新的等待当前请求的空队列添加到当前请求缓存队列中
mWaitingRequests.put(cacheKey, null);
mCacheQueue.add(request);
}
return request;
}
}
****详细分析:** 有两个队列需要说明一下,mCurrentRequests正在被请求的队列,在调用上述add的时候,需将当前请求加入到此队列,主要作用是取消请求;另一个是mWaitingRequests,作用是避免多次发送相同请求,若当前没有相同请求正在处理,则将当前请求key中加入空队列;当请求结束时,移除mWaitingRequests中的当前请求key中的等待请求,并将当前key中的所有等待请求加入到缓存队列(可见RequestQueue中的finish)。
(3)NetworkDispatcher和CacheDispatcher
上面说到请求加入队列后会有相应的调度线程来处理,这也是Volley中最重要的部分
NetworkDispatcher.java 先看一下网络调度线程;在run中执行while(true),若网络队列中没有元素则阻塞队列,直到队列有元素加入,取出元素request,通过网络请求,将返回的response加入缓存(若需缓存)且将结果分发给上层回调。
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Request<?> request;
while (true) {
long startTimeMs = SystemClock.elapsedRealtime();
// release previous request object to avoid leaking request object when mQueue is drained.
request = null;
try {
// Take a request from the queue.
request = mQueue.take();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
try {
request.addMarker("network-queue-take");
// If the request was cancelled already, do not perform the
// network request.
// 如果请求被中途取消
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
continue;
}
// 流量统计用的
addTrafficStatsTag(request);
// Perform the network request.
// 请求数据
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");
// If the server returned 304 AND we delivered a response already,
// we're done -- don't deliver a second identical response.
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
//如果是相同的请求,那么服务器就返回一次响应
request.finish("not-modified");
continue;
}
// Parse the response here on the worker thread.
// 解析响应数据
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");
// Write to cache if applicable.
// TODO: Only update cache metadata instead of entire record for 304s.
if (request.shouldCache() && response.cacheEntry != null) {
// 缓存数据
mCache.put(request.getCacheKey(), response.cacheEntry);
request.addMarker("network-cache-written");
}
// Post the response back.
//确认请求要被分发
request.markDelivered();
//发送请求
mDelivery.postResponse(request, response);
} catch (VolleyError volleyError) {
volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
VolleyError volleyError = new VolleyError(e);
volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
mDelivery.postError(request, volleyError);
}
}
}
CacheDispatcher.java 接下来看一下缓存调度线程;在run中执行while(true),若缓存队列中没有元素则阻塞队列,直到队列有元素加入,取出元素request,通过key取出缓存,若缓存无数据或者已过期则加入网络队列进行网络请求,否则取出缓存封装成response;若缓存不需要更新则直接分发给上层回调,否则再次提交网络请求。
@Override
public void run() {
if (DEBUG) VolleyLog.v("start new dispatcher");
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// Make a blocking call to initialize the cache.
// 初始化缓存
mCache.initialize();
Request<?> request;
while (true) {
// release previous request object to avoid leaking request object when mQueue is drained.
request = null;
try {
// Take a request from the queue.
// 从缓存队列中取出请求
request = mCacheQueue.take();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
try {
request.addMarker("cache-queue-take");
// If the request has been canceled, don't bother dispatching it.
// 取消请求
if (request.isCanceled()) {
request.finish("cache-discard-canceled");
continue;
}
// Attempt to retrieve this item from cache.
Cache.Entry entry = mCache.get(request.getCacheKey());
//缓存已过期(包括expired与Soft-expired)
// 无缓存数据,则加入网络请求
if (entry == null) {
request.addMarker("cache-miss");
// Cache miss; send off to the network dispatcher.
mNetworkQueue.put(request);
continue;
}
// If it is completely expired, just send it to the network.
// 判断缓存的新鲜度,过期了,加入网络请求
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
}
// We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
//从缓存中取出请求响应并进行解析
Response<?> response = request.parseNetworkResponse(new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");
//判断缓存是需要刷新
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
// 缓存没有Soft-expired,则直接通过mDelivery将解析好的结果交付给请求发起者
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
// Mark the response as intermediate.
response.intermediate = true;
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
final Request<?> finalRequest = request;
//需要刷新,那么就再次提交网络请求...获取服务器的响应...
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
// 网络来更新请求响应
mNetworkQueue.put(finalRequest);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
}
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
}
}
}
****详细分析:** 有两个队列需要说明一下,mCurrentRequests正在被请求的队列,在调用上述add的时候,需将当前请求加入到此队列,主要作用是取消请求;另一个是mWaitingRequests,作用是避免多次发送相同请求,若当前没有相同请求正在处理,则将当前请求key中加入空队列;当请求结束时,移除mWaitingRequests中的当前请求key中的等待请求,并将当前key中的所有等待请求加入到缓存队列(可见RequestQueue中的finish)。
(4)BasicNetwork.java
封装HttpStack网络请求,实际由HttpStack实现。
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
//获取请求开始的时间 debug用的
long requestStart = SystemClock.elapsedRealtime();
//如果发生超时,认证失败等错误,进行重试操作,直到成功、抛出异常(不满足重试策略等)结束
while (true) {
HttpResponse httpResponse = null;
//请求内容对象
byte[] responseContents = null;
//用于保存响应数据报的Header中的数据
Map<String, String> responseHeaders = Collections.emptyMap();
try {
// Gather headers. 保存缓存下来的Header
Map<String, String> headers = new HashMap<String, String>();
// 添加请求头部的过程
addCacheHeaders(headers, request.getCacheEntry());
// 执行请求
httpResponse = mHttpStack.performRequest(request, headers);
//获取响应状态
StatusLine statusLine = httpResponse.getStatusLine();
//响应状态码
int statusCode = statusLine.getStatusCode();
//获取响应后的Header中的所有数据
responseHeaders = convertHeaders(httpResponse.getAllHeaders());
// Handle cache validation.
// 304:表示从上次访问后,服务器数据没有改变,则从Cache中拿数据
if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
Entry entry = request.getCacheEntry();
if (entry == null) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null, responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// A HTTP 304 response does not have all header fields. We
// have to use the header fields from the cache entry plus
// the new ones from the response.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
entry.responseHeaders.putAll(responseHeaders);
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data, entry.responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// Handle moved resources
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
String newUrl = responseHeaders.get("Location");
request.setRedirectUrl(newUrl);
}
// Some responses such as 204s do not have content. We must check.
if (httpResponse.getEntity() != null) {
responseContents = entityToBytes(httpResponse.getEntity());
} else {
// Add 0 byte response as a way of honestly representing a
// no-content request.
//由于204响应时不返回数据信息的,返回空数据
responseContents = new byte[0];
}
// if the request is slow, log it.
long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
//如果一个请求的时间超过了指定的缓慢请求时间,那么需要显示这个时间,debug
logSlowRequests(requestLifetime, request, responseContents, statusLine);
//如果请求状态出现错误,bug
//if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT) {
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
// 返回header+body数据
return new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (ConnectTimeoutException e) {
attemptRetryOnException("connection", request, new TimeoutError());
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
int statusCode = 0;
NetworkResponse networkResponse = null;
if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
} else {
throw new NoConnectionError(e);
}
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
VolleyLog.e("Request at %s has been redirected to %s", request.getOriginUrl(), request.getUrl());
} else {
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
}
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
//请求需要进行验证,或者是需要授权异常处理
if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
//尝试重试策略方法
attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
} else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
attemptRetryOnException("redirect", request, new RedirectError(networkResponse));
} else {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(e);
}
}
}
}
****详细分析:** 在网络分发器中用到了这里,主要通过这个方法来获取请求结果。此方法中也有个while(true)循环,如果发生超时,认证失败等错误,进行重试操作,直到成功、抛出异常(不满足重试策略等)结束;实际由HttpStack执行请求,将请求结果封装到NetworkResponse中(之后通过Request子类将其转成需要的类型分发给上层回调)。
(5)HttpClientStack和HurlStack
在上述调度线程中,实际处理请求获取到请求的结果的类就是这两个,这两个类同implements了HttpStack接口,这个接口只有一个方法需要实现,performRequest处理请求。
HurlStack.java performRequest内由HttpURLConnection处理网络请求,返回response
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError { String url = request.getUrl();
//添加head:请求head+额外的head
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
//目前没有使用到,对url进行重写,重写的好处使url更加的保密,安全
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
// 建立连接
HttpURLConnection connection = openConnection(parsedUrl, request);
// 设置请求的相关属性
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
//请求的方式去执行相关的方法
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
//获取协议版本
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
//响应码
int responseCode = connection.getResponseCode();
// 有的接口返回401,connection.getResponseCode(responseCode=401)就会抛出IOException
// 解决方案 : http://blog.csdn.net/kufeiyun/article/details/44646145
// http://stackoverflow.com/questions/30476584/android-volley-strange-error-with-http-code-401-java-io- ioexception-no-authe
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
//响应信息
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
//响应状态
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
//获取响应中的实体
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
HttpClientStack performRequest内由HttpClient处理网络请求,返回response
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
//创建请求
HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
//添加head
addHeaders(httpRequest, additionalHeaders);
addHeaders(httpRequest, request.getHeaders());
//准备请求回调
onPrepareRequest(httpRequest);
HttpParams httpParams = httpRequest.getParams();
int timeoutMs = request.getTimeoutMs();
// TODO: Reevaluate this connection timeout based on more wide-scale
// data collection and possibly different for wifi vs. 3G.
// 设置连接超时时间5S
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
// 设置请求超时,默认设置2.5s
HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
// 执行
return mClient.execute(httpRequest);
}
(6)ResponseDelivery和ExecutorDelivery
在调度线程中获取到结果后,当然就需要分发结果返回给上层回调处理,分发器就是ExecutorDelivery,其中ResponseDelivery是ExecutorDelivery需要实现的接口,此接口中只包含请求成功和失败的处理方法。在ExecutorDelivery中使用了线程池的管理,不用每次创建新的线 程,重复使用已有线程即可,节省内存。
public ExecutorDelivery(final Handler handler) {
// Make an Executor that just wraps the handler.
mResponsePoster = new Executor() {
@Override
public void execute(Runnable command) {
//传入的Handler与主线程绑定了,所以分发消息是在主线程中
handler.post(command);
}
};
}
在主线程的runnable中分发网络请求结果给listener 将网络请求的结果返回给上层回调;若有附加线程则启动,主要在缓存调度器中使用,若需要刷新请求则在附件线程中重新执行网络请求。
private class ResponseDeliveryRunnable implements Runnable {
/**
* 请求
*/
private final Request mRequest;
/**
* 响应
*/
private final Response mResponse;
/**
* 其他线程
*/
private final Runnable mRunnable;
public ResponseDeliveryRunnable(Request request, Response response, Runnable runnable) {
mRequest = request;
mResponse = response;
mRunnable = runnable;
}
@SuppressWarnings("unchecked")
@Override
public void run() {
// If this request has canceled, finish it and don't deliver.
// 如果请求被中断,那么就不需要发送响应了
if (mRequest.isCanceled()) {
mRequest.finish("canceled-at-delivery");
return;
}
// Deliver a normal response or error, depending.
// 如果服务器响应成功,中途没有错误的发生
if (mResponse.isSuccess()) {
// 将服务器返回的结果发送给客户端
mRequest.deliverResponse(mResponse.result);
} else {
// 把错误发送给客户端
mRequest.deliverError(mResponse.error);
}
// If this is an intermediate response, add a marker, otherwise we're done
// and the request can be finished.
// 中间响应
if (mResponse.intermediate) {
mRequest.addMarker("intermediate-response");
} else {
//请求结束
mRequest.finish("done");
}
// If we have been provided a post-delivery runnable, run it.
// 启动附加线程
if (mRunnable != null) {
mRunnable.run();
}
}
}
(7)Request
接下来看一下我们一直提到的Request,Volley中Request的子类:ClearCacheRequest、JsonRequest、JsonArrayRequest、JsonObjectRequest、ImageRequest、StringRequest 由于调度队列中的请求队列和网络队列都是优先级队列,其中的元素就是Request,故Request实现了Comparable接口,即需要实现compareTo,此方法主要作用是排列请求的优先级:优先级越高越前、优先级相同则根据序号来排、序号越低越前。 Request中有两个方法需要子类实现。parseNetworkResponse(NetworkResponse response):将原始的请求结果封装成所需要的数据类型;deliverResponse:将请求结果分发给上层回调处理。 各子类的实现较为简单,就不做详细说明了。
@Override
public int compareTo(Request<T> other) {
Priority left = this.getPriority();
Priority right = other.getPriority();
//优先级越高,在请求队列中排得越前,相同优先级的序号越低,排得越前。
// High-priority requests are "lesser" so they are sorted to the front.
// Equal priorities are sorted by sequence number to provide FIFO ordering.
return left == right ?
this.mSequence - other.mSequence :
right.ordinal() - left.ordinal();
//ordinal():回此枚举常量的序数,从0开始,下标
}