Volley Why not Threadpoolexecutor

Source: Internet
Author: User

Many people have seen the source of volley, there will be a confusion, the implementation of network communication operations of the 4 threads are managed by the array, no use to threadpoolexecutor.

Paste Code Requestqueue.start (), which is the starting point for the network request:

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 ();         }    }

Networkdispather instances are managed directly using arrays. Networkdispather is the thread of the network request.

Then why not threadpoolexecutor. I personally think this may be the use of threadpoolexecutor, thepurpose is to thread reuse, improve efficiency. But read the volley in the network thread Networkdispatcher source code, I know that in this scenario is not suitable for threadpoolexecutor, paste networkdispatcher source code, mainly the Run method:

  public void run ()  {         Process.setthreadpriority (Process.thread_priority_background);         request<?> request;        while  (True)  {             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 ();       &nbSp;         mdelivery.postresponse (Request, response);             } catch  (volleyerror  Volleyerror)  {                 parseanddelivernetworkerror (Request, volleyerror);             } catch  (exception e)  {        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VOLLEYLOG.E (e,  "unhandled exception %s",  e.tostring ());                 mdelivery.posterror (Request, new volleyerror (e));             }        }    }

You can see that the Run method is a while (true), the thread is designed to survive the thread, loop to execute the request, if there is no data in the queue, the thread will always block at Mqueue.take (), this design idea and the Android message queue similar, The message queue core is also the data that is taken from the queue through a dead loop, and then the operation is performed. Based on Threadpoolexecutor's usage scenarios and code analysis, it concludes that volley does not require thread pooling technology. In fact, a networkdispatcher also enough, designed to 4 networkdispatcher asynchronous execution, mainly in order to improve performance, after all, the network request cost is basically greater than the processing of Message Queuing.

Volley Why not Threadpoolexecutor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.