@ SuppressWarnings ( "unchecked" ) public void parallel ( C context , final Outcome < C > outcome , final Function < C > ... functions ) { final C finalContext = context != null ? context : ( C ) EMPTY_CONTEXT ; final CountingControl ctrl = new CountingControl ( finalContext , functions ) ; progress . reset ( functions . length ) ; Scheduler . get ( ) . scheduleIncremental ( new Scheduler . RepeatingCommand ( ) { @ Override public boolean execute ( ) { if ( ctrl . isAborted ( ) || ctrl . allFinished ( ) ) { // schedule deferred so that 'return false' executes first! Scheduler . get ( ) . scheduleDeferred ( new Scheduler . ScheduledCommand ( ) { @ Override @ SuppressWarnings ( "unchecked" ) public void execute ( ) { if ( ctrl . isAborted ( ) ) { progress . finish ( ) ; outcome . onFailure ( finalContext ) ; } else { progress . finish ( ) ; outcome . onSuccess ( finalContext ) ; } } } ) ; return false ; } else { // one after the other until all are active ctrl . next ( ) ; return true ; } } } ) ; }
Run an array of functions in parallel without waiting until the previous function has completed . If any of the functions pass an error to its callback the outcome is immediately called with the value of the error .