Subtotal, thread pool

Source: Internet
Author: User

Thread pool Management process: first create a thread pool, and then gradually increase the thread to corepoolsize based on the number of tasks, and if there are still additional tasks at this point, place them in workqueue until Workqueue is full. Then continue to increase the number of threads in the pool (enhanced processing power) and eventually reach Maxinumpoolsize. What if there's a task to add at this point? This requires handler to dispose of, or discard new tasks, or reject new tasks, or squeeze out existing tasks. In the case of both the task queue and the thread pool are saturated, once the thread is waiting (the task is finished and no new task) is over KeepAliveTime, then it terminates, that is, the number of threads in the pool will gradually decrease until the number of corepoolsize is reached.

Newfixedthreadpool Fixed thread pool usage

Introduction to the scene

    在一个业务逻辑中需要同时生成7份合同(pdf)格式,在生成合同的同时,    不仅需要调用一些外部接口去获取合同中的参数信息,而且还需要调用个人签章    和企业签章去合同中生成。这样调用一个合同的时间就很长了,一个大概也需要3-4s,    那么如果顺序执行7个,完成就需要20几s。这样接口响应就会需要很长时间来完成。

Solutions

    1/异步调用生成合同接口    2/在异步调用接口的同时,采用线程池的方式同时创建7个线程去同时生成7个合同。    这样既省时间又可以很快给用户作出响应。

Executorservice executor = Executors.newfixedthreadpool (7); future<boolean> f = executor.submit((), {try{Return Generatepuchase (CONTRACTNO); }catch (Exception e) {logger.error ("Failed to generate the consulting service agreement", e);ReturnFalse } });future<Boolean>f1=Executor.Submit((), {try{Return generateentrustment (CONTRACTNO); }catch (Exception e) {logger.error ("Failure to generate a deduction agreement", e);ReturnFalse } });future<Boolean>F2=Executor.Submit((), {try{Return Generatepromise (CONTRACTNO); }catch (Exception e) {logger.error ("Failed to generate user commitment letter", e);ReturnFalse } });future<Boolean>f3=Executor.Submit((), {try{Return generateuserconfirm (CONTRACTNO); }catch (Exception e) {logger.error ("Failed to generate user confirmation", e);ReturnFalse } });future<Boolean>f4=Executor.Submit((), {try{Return Generatetrust (CONTRACTNO); }catch (Exception e) {logger.error ("Failure to generate the trust agreement", e);ReturnFalse } });future<Boolean>f5=Executor.Submit((), {try{Return generatehjconfirm (CONTRACTNO); }catch (Exception e) {logger.error ("Failed to generate user confirmation", e);ReturnFalse } });future<Boolean>f6=Executor.Submit((), {try{Return Generatelend (CONTRACTNO); }catch (Exception e) {logger.error ("Failure to generate a loan contract", E);ReturnFalse } });For(;;) {If(F.isdone () && f1.isdone () && f2.isdone () && f3.isdone () && f4.isdone () && F5.isdone () && F6.isdone ()) {try {If(F.get () && f1.get () && f2.get () && f3.get () && f4.get () && f5.get () && f6 . Get ()) {//7 contracts are generated at the same time before calling contract borrowing 2//Call the signing interfaceStringSignurl=insideserviceurl+Commonconstants.inside_fr_sign_url+OrderId;Logger.Info("Call contract Address {}", Signurl);Commonresponseresultcommonresponseresult=Oauth2resttemplate.Getforobject(Signurl,commonresponseresult.class);If("00000". Equals (Commonresponseresult.getcode ())) {Logger.Info( "Order ID for {} Call Signing interface succeeded", OrderId); }else if ( "99999". Equals (Commonresponseresult.getcode ())) {logger. Info ( "Order id = {} Call Signing interface failed", orderId);} break;} } catch  (interruptedexception e) { Logger. error ( "Thread execution error 1", e);} catch  (executionexception e) {logger.error (" Thread execution error 2 ", e);}}      
Simple parsing: The For loop requires a continuous loop to wait for all 7 threads to complete and the contract interface to be invoked only if the return result is all true. No problem with the pro test, soon
of which: future. Get ()
Waits if necessary for the computation to complete, and then retrieves its result.
 

Subtotal, thread pool

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.