做解析会员电影的网站,电子商城网站如何建设,软件开发费用一览表,手机网站 jquery 特效1.正在执行的任务数量最大值是64 异步请求放入readyAsyncCalls后#xff0c;遍历readyAsyncCalls取出任务去执行的时候#xff0c;如果发现runningAsyncCalls的数量大于等于64#xff0c;就不从readyAsyncCalls取出任务执行。
public final class Dispatcher {private int …1.正在执行的任务数量最大值是64 异步请求放入readyAsyncCalls后遍历readyAsyncCalls取出任务去执行的时候如果发现runningAsyncCalls的数量大于等于64就不从readyAsyncCalls取出任务执行。
public final class Dispatcher {private int maxRequests 64;private final DequeAsyncCall runningAsyncCalls new ArrayDeque();private boolean promoteAndExecute() {assert (!Thread.holdsLock(this));ListAsyncCall executableCalls new ArrayList();boolean isRunning;synchronized (this) {for (IteratorAsyncCall i readyAsyncCalls.iterator(); i.hasNext(); ) {AsyncCall asyncCall i.next();//如果超过了最大数目if (runningAsyncCalls.size() maxRequests) break; // Max capacity.if (asyncCall.callsPerHost().get() maxRequestsPerHost) continue; // Host max capacity.//从readyAsyncCalls removei.remove();//callsPerHost1asyncCall.callsPerHost().incrementAndGet();//添加到executableCallsexecutableCalls.add(asyncCall);//添加到runningAsyncCallsrunningAsyncCalls.add(asyncCall);}isRunning runningCallsCount() 0;}for (int i 0, size executableCalls.size(); i size; i) {AsyncCall asyncCall executableCalls.get(i);//执行asyncCall.executeOn(executorService());}return isRunning;}
}
2.同一个主机的最大连接数为5
异步请求放入readyAsyncCalls后遍历readyAsyncCalls取出任务去执行的时候如果发现asyncCall的callsPerHost大于等于5就不从readyAsyncCalls取出任务执行否则callsPerHost加1。
public final class Dispatcher {private int maxRequestsPerHost 5; //默认5。这是okhttp对同一主机允许的最大请求数量。void enqueue(AsyncCall call) {synchronized (this) {readyAsyncCalls.add(call);//Mutate the AsyncCall so that it shares the AtomicInteger //of an existing running call to the same host.if (!call.get().forWebSocket) {//从已经存在的任务里面找同一个主机的任务AsyncCall existingCall findExistingCallWithHost(call.host());if (existingCall ! null) {//call的将callsPerHost赋值为existingCall的callsPerHostcall.reuseCallsPerHostFrom(existingCall);}}}promoteAndExecute();}//有个疑问这里是不是要从ArrayDeque尾向前获取才能获取到最新的AsyncCall这样获取到的//callsPerHost才会是最大的//目前从头开始获取是不是有问题//先从runningAsyncCalls找再从readyAsyncCalls找Nullable private AsyncCall findExistingCallWithHost(String host) {for (AsyncCall existingCall : runningAsyncCalls) {if (existingCall.host().equals(host)) {return existingCall;}}for (AsyncCall existingCall : readyAsyncCalls) {if (existingCall.host().equals(host)) {return existingCall;}}return null;}
}