public int execute ( ) { int executed = 0 ; final long now = now ( ) ; sortIfNeeded ( ) ; Set < Ticket > cancelled = new HashSet <> ( ) ; for ( Ticket ticket : this . tickets ) { if ( now - ticket . start < ticket . delay ) { // tickets are ordered, not meeting the condition means the next ones do not as well break ; } if ( ! ticket . alive ) { // Dead ticket, let's continue cancelled . add ( ticket ) ; continue ; } ticket . alive = false ; cancelled . add ( ticket ) ; ticket . handler . time ( ticket . args ) ; ++ executed ; } for ( int idx = tickets . size ( ) ; idx -- > 0 ; ) { Ticket ticket = tickets . get ( idx ) ; if ( ticket . alive ) { break ; } cancelled . add ( ticket ) ; } this . tickets . removeAll ( cancelled ) ; cancelled . clear ( ) ; return executed ; }
Execute the tickets .