Threadpoolexecutor Source Learning (2)--Application in Thrift

Source: Internet
Author: User

Thrift as a bottom-up to remove the business logic code, can generate a variety of language client and server code, covering the network, IO, process, thread management framework, really huge, but it is clear level, 4 layers each layer to solve different problems, can be used on demand, quite convenient.

+-------------------------------------------+| Server                                    |  -- server process Scheduling | (single-threaded, Event-driven etc)       | +-------------------------------------------+| Processor                                 |  -- RPC Interface processing function distribution, IDL definition interface implementation will be attached to this | (compiler Generated)                      | +-------------------------------------------+| Protocol                                  |  -- Agreement | (JSON, compact etc)                       | +-------------------------------------------+| Transport                                 |  -- Network Transmission | (Raw TCP, HTTP etc)                       | +-------------------------------------------+

In fact, for the service-side programming technology Daniel, server scheduling may best reflect the personal skills, but from the transport layer, to serialize this layer of work, is indeed a relatively tedious work, you can directly use thrift generated code to complete the problem.

Above is a digression, in Thrift Java Code Implementation Server This layer has a tthreadpoolserver, inside for thread management is to use Threadpoolexecutor, the following paste the core code

   Public voidserve () {Try{Servertransport_.listen (); } Catch(ttransportexception ttx) {logger.error ("Error occurred during listening.", TTX); return; } stopped_=false;  while(!stopped_) {      intFailurecount = 0; Try{Ttransport Client=servertransport_.accept (); workerprocess WP=Newworkerprocess (client); Executorservice_.execute (WP);//This is threadpoolexecutor}Catch(ttransportexception ttx) {if(!stopped_) {          ++Failurecount; Logger.warn ("Transport error occurred during acceptance of message.", TTX);    }}} executorservice_.shutdown (); //Loop until Awaittermination finally does return without a interrupted//exception. If we don ' t do this, then we'll shut down prematurely. We want//to let the executorservice clear it ' s task queue, closing client sockets//appropriately.    LongTimeoutms =Options_.stopTimeoutUnit.toMillis (Options_.stoptimeoutval); Longnow =System.currenttimemillis ();  while(Timeoutms >= 0) {      Try{executorservice_.awaittermination (Timeoutms, timeunit.milliseconds);  Break; } Catch(interruptedexception ix) {LongNewnow =System.currenttimemillis (); Timeoutms-= (Newnow-Now ); now=Newnow; }    }  }

It is worth noting that the shutdown () method needs to be called when all tasks are performed, this is a lot of examples on the net, but for the last paragraph of the author repeatedly check the state and then exit, this is really not necessary, in the shutdown () method has a similar code (jdk1.7); Furthermore, Java does not affect other threads when the main thread exits, so this code is more redundant:-D

Threadpoolexecutor Source Learning (2)--Application in Thrift

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.