正文
));
}
}
BasicNetwork
network1
=
new
BasicNetwork
((
HttpStack
)
stack
);
RequestQueue
queue1
;
if
(
maxDiskCacheBytes
1
)
{
queue1
=
new
RequestQueue
(
new
DiskBasedCache
(
cacheDir
),
network1
);
}
else
{
queue1
=
new
RequestQueue
(
new
DiskBasedCache
(
cacheDir
,
maxDiskCacheBytes
),
network1
);
}
queue1
.
start
();
return
queue1
;
}
可以看到如果android版本大于等于2.3则调用基于HttpURLConnection的HurlStack,否则就调用基于HttpClient的HttpClientStack。并创建了RequestQueue,调用了start()方法:
public
void
start
()
{
this
.
stop
();
this
.
mCacheDispatcher
=
new
CacheDispatcher
(
this
.
mCacheQueue
,
this
.
mNetworkQueue
,
this
.
mCache
,
this
.
mDelivery
);
this
.
mCacheDispatcher
.
start
();
for
(
int
i
=
0
;
i
this
.
mDispatchers
.
length
;
++
i
)
{
NetworkDispatcher
networkDispatcher
=
new
NetworkDispatcher
(
this
.
mNetworkQueue
,
this
.
mNetwork
,
this
.
mCache
,
this
.
mDelivery
);
this
.
mDispatchers
[
i
]
=
networkDispatcher
;
networkDispatcher
.
start
();
}
}
CacheDispatcher是缓存调度线程,并调用了start()方法,在循环中调用了NetworkDispatcher的start()方法,NetworkDispatcher是网络调度线程,默认情况下mDispatchers.length为4,默认开启了4个网络调度线程,也就是说有5个线程在后台运行并等待请求的到来。接下来我们创建各种的Request,并调用RequestQueue的add()方法:
public
Request
add
(
Request
request
)
{
request
.
setRequestQueue
(
this
);
Set
var2