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