Public Requestqueue Mrequestqueue = Volley.newrequestqueue (Getapplicationcontext ());
Everything starts with this code ...
Follow Newrequestqueue (context) into the volley class
Public class Volley { ... Public Static Requestqueue Newrequestqueue (context context) { returnnull); } }
1 Public StaticRequestqueue Newrequestqueue (context context, Httpstack stack) {2File Cachedir =NewFile (Context.getcachedir (), default_cache_dir);3 4String useragent = "volley/0";5 Try {6String PackageName =context.getpackagename ();7PackageInfo info = Context.getpackagemanager (). Getpackageinfo (PackageName, 0);8useragent = PackageName + "/" +Info.versioncode;9}Catch(namenotfoundexception e) {Ten } One A if(Stack = =NULL) {//You start to pass NULL, so go here - if(Build.VERSION.SDK_INT >= 9) { -stack =Newhurlstack ();//SDK if it is greater than or equal to 9, which is Android 2.3, since the introduction of HttpURLConnection the}Else{//if less than 9, it is implemented with HttpClient - //Prior to Gingerbread, HttpURLConnection was unreliable. - //See :http://android-developers.blogspot.com/2011/09/androids-http-clients.html -stack =NewHttpclientstack (Androidhttpclient.newinstance (useragent)); + } - } + ANetwork Network =Newbasicnetwork (stack); atCreate a network, the parameter stack is used to communicate -Requestqueue queue =NewRequestqueue (NewDiskbasedcache (cachedir), network); - Queue.start (); - - returnqueue; -}
Then there's Requestqueue, F2.
Publicrequestqueue (cache cache, network network) { This(cache, network, default_network_thread_pool_size); } ... PublicRequestqueue (cache cache, network network,intThreadPoolSize) { This(cache, network, ThreadPoolSize,NewExecutordelivery (NewHandler (Looper.getmainlooper ())); } ... PublicRequestqueue (cache cache, network network,intThreadPoolSize, Responsedelivery delivery) {Mcache=Cache; Private final cache Mcache for cache mnetwork=Network; Private Final network mnetwork to connect to the network mdispatchers=NewNetworkdispatcher[threadpoolsize]; /networkdispatcher inherits thread, network distributes thread pool Mdelivery=delivery; Delivery Response Implementation}
Of the above four parameters, the first two are incoming parameters, the last two parameters, the number of ThreadPoolSize thread pools, the default is 4, leaving the final one, into Executordelivery (new Handler (Looper.getmainlooper ()))
Public Executordelivery (final Handler Handler) { // make a Executor that just wraps the hand Ler. New Executor () { @Override publicvoid execute (Runnable command) { Handler.post (command);}} ; }
You can see that handler the response back to the main thread for updates.
Since then, the code requestqueue queue = new Requestqueue (new Diskbasedcache (CACHEDIR), network), has been executed, the next sentence is Queue.start ()
Look at the code for start, as follows
/*** Starts the dispatchers in this queue. */ Public voidstart () {Stop (); //Make sure any currently running dispatchers is stopped. //Create the cache dispatcher and start it.Mcachedispatcher =NewCachedispatcher (Mcachequeue, Mnetworkqueue, Mcache, mdelivery); Mcachedispatcher.start (); //Create Network dispatchers (and corresponding threads) up to the pool size. for(inti = 0; i < mdispatchers.length; i++) {Networkdispatcher Networkdispatcher=NewNetworkdispatcher (Mnetworkqueue, Mnetwork, Mcache, mdelivery); Mdispatchers[i]=Networkdispatcher; Networkdispatcher.start (); } } /*** Stops the cache and network dispatchers. */ Public voidStop () {if(Mcachedispatcher! =NULL) {mcachedispatcher.quit (); } for(inti = 0; i < mdispatchers.length; i++) { if(Mdispatchers[i]! =NULL) {mdispatchers[i].quit (); } } }
As you can see, the start function calls stop to exit all worker threads for Mcachedispatcher and mdispatchers, ensuring that the currently running dispatch thread stops later, and then re-creates and starts the dispatch thread.
Mdispatchers well understood, that is, requestqueue initialized network distribution thread pool, that mcachedispatcher is what it, literally means cache dispatcher (dispatch)
Look at the four parameters in his constructor, and the latter two Mcache and mdelivery are the arguments passed in from the first sentence, which is better understood, leaving the previous two parameters: Mcachequeue and Mnetworkqueue
It can be found in the Requestqueue.java two definitions, as follows:
/** the cache triage queue. */ private final priorityblockingqueue<request<?>> mcachequeue = new Priorityblock Ingqueue<request<?>> (); /** The queue of requests that is actually Going out to the network. */ private final priorityblockingqueue<request<?>> mnetworkqueue = new priorityblockingqueue<request<?>> ();
Priorityblockingqueue is provided by Java and contracted java.util.concurrent, and tasks can be performed synchronously by priority using Priorityblockingqueue. Look at the definition of generics as request to think that these two queues should be used to store cache requests and network requests.
Since the first sentence of this volley has been executed and returned to a requestqueue, let me summarize what the first line of code does:
1. The first step is to define the cache directory Cachedir from the incoming parameter context, the package name version number useragent,httpstack the protocol stack, create a network interface using the protocol stack, create a hard disk cache with the cache directory
2. The network and cache incoming Requestqueue create the request queue, which after the 2-step constructor of the overloaded jump, the new addition of the net distribution thread pool Mdispatchers and answer the Mdelivery, Four parameter implementations complete the initialization of the requestqueue.
After the 3.requestqueue initialization is complete, call start to start the request queue for formal work. Where start first puts all mcachedispatcher and mdispatchers worker threads out of the requestqueue and reloads them. Both of them need to be created using Requestqueue internal private members priorityblockingqueue<request<?>> Mcachequeue and Priorityblockingqueue <Request<?>> Mnetworkqueue.
From the actual working principle of mcachequeue and mnetworkqueue, we have to separate record ~
Android.volley's interpretation: initial contact volley