private void runTasks ( ) { //don't run anything in the IO thread till the tasks are done delegate . getSinkChannel ( ) . suspendWrites ( ) ; delegate . getSourceChannel ( ) . suspendReads ( ) ; List < Runnable > tasks = new ArrayList <> ( ) ; Runnable t = engine . getDelegatedTask ( ) ; while ( t != null ) { tasks . add ( t ) ; t = engine . getDelegatedTask ( ) ; } synchronized ( this ) { outstandingTasks += tasks . size ( ) ; for ( final Runnable task : tasks ) { getWorker ( ) . execute ( new Runnable ( ) { @ Override public void run ( ) { try { task . run ( ) ; } finally { synchronized ( SslConduit . this ) { if ( outstandingTasks == 1 ) { getWriteThread ( ) . execute ( new Runnable ( ) { @ Override public void run ( ) { synchronized ( SslConduit . this ) { SslConduit . this . notifyAll ( ) ; -- outstandingTasks ; try { doHandshake ( ) ; } catch ( IOException | RuntimeException | Error e ) { IoUtils . safeClose ( connection ) ; } if ( anyAreSet ( state , FLAG_READS_RESUMED ) ) { wakeupReads ( ) ; //wakeup, because we need to run an unwrap even if there is no data to be read } if ( anyAreSet ( state , FLAG_WRITES_RESUMED ) ) { resumeWrites ( ) ; //we don't need to wakeup, as the channel should be writable } } } } ) ; } else { outstandingTasks -- ; } } } } } ) ; } } }
Execute all the tasks in the worker