[Weave message Frame] [Netty Source Code Analysis]9 Promise implementation Class defaultpromise responsibilities and implementation

Source: Internet
Author: User

Netty Future is based on the JDK future extension to monitor the completion of task triggering execution
Promise is to modify the task data for the future
Defaultpromise is an important template class, other different types of implementations are basically a simple wrapper, such as Defaultchannelpromise
The main analysis is how await is waiting for the results.

 Public InterfaceFuture<v>extendsJava.util.concurrent.future<v>{ Future<V> AddListener (genericfuturelistener<?extendsfuture<?SuperV>>listener);} Public InterfacePromise<v>extendsFuture<v>{Promise<V>setsuccess (V result); Booleantrysuccess (V result); Promise<V>setfailure (throwable cause); Booleantryfailure (throwable cause); Booleansetuncancellable ();} Public classDefaultpromise<v>extendsAbstractfuture<v>ImplementsPromise<v>{@Override Public BooleanAwaitLongTimeout, timeunit unit)throwsinterruptedexception {returnAwait0 (Unit.tonanos (timeout),true); }    Private BooleanAwait0 (LongTimeoutnanos,Booleaninterruptable)throwsinterruptedexception {//completed tasks are ignored directly        if(IsDone ()) {return true; }        //no waiting time to return processing records        if(Timeoutnanos <= 0) {            returnIsDone (); }        //Aborted Throw exception        if(Interruptable &&thread.interrupted ()) {            Throw Newinterruptedexception (toString ()); }        //Checkdeadlock (); //Netty that the current thread is a deadlock stateEventexecutor e =executor (); if(E! =NULL&&E.ineventloop ()) {            Throw Newblockingoperationexception (toString ()); }                LongStartTime =System.nanotime (); LongWaitTime =Timeoutnanos; Booleaninterrupted =false; Try {             for (;;) {                synchronized( This) {                    if(IsDone ()) {return true; }                    //Maximum Check count is Short.max_value//very strange logic, after processing and self-reduction                    if(Waiters = =short.max_value) {                        Throw NewIllegalStateException ("Too many waiters:" + This); }                    ++Waiters; Try {                        //the blocked code is just a row of parameter 1 is milliseconds, parameter 2 is auxiliary with greater than 0 o'clock Milliseconds+1, if 0, will be unrestricted blockingWait (waittime/1000000, (int) (WaitTime% 1000000)); } Catch(interruptedexception e) {if(interruptable) {Throwe; } Else{interrupted=true; }                    } finally{Waiters--; }                }                //here is a double check with concurrency-free logic placed outside the synchronized                if(IsDone ()) {return true; } Else{waitTime= Timeoutnanos-(System.nanotime ()-startTime); if(waitTime <= 0) {                        returnIsDone (); }                }            }        } finally {            if(interrupted) {Thread.CurrentThread (). interrupt (); }        }    }} Public classDefaultchannelpromiseextendsDefaultpromise<void>Implementschannelpromise, Flushcheckpoint {Private Finalchannel Channel;  PublicDefaultchannelpromise (Channel channel) { This. Channel =Channel; }     PublicDefaultchannelpromise (channel channel, Eventexecutor executor) {Super(executor);  This. Channel =Channel; }}

[Weave message Frame] [Netty Source Code Analysis]9 Promise implementation Class defaultpromise responsibilities and implementation

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.