private void processNow ( ) { List < T > toProcess ; boolean scheduleNextBatchImmediately = false ; synchronized ( mutex ) { int count = inbox . size ( ) ; Log . v ( Log . TAG_BATCHER , "%s: processNow() called, inbox size: %d" , this , count ) ; if ( count == 0 ) return ; else if ( count <= capacity ) { toProcess = new ArrayList < T > ( inbox ) ; inbox . clear ( ) ; } else { toProcess = new ArrayList < T > ( inbox . subList ( 0 , capacity ) ) ; for ( int i = 0 ; i < capacity ; i ++ ) inbox . remove ( 0 ) ; scheduleNextBatchImmediately = true ; } mutex . notifyAll ( ) ; } synchronized ( processMutex ) { if ( toProcess != null && toProcess . size ( ) > 0 ) { Log . v ( Log . TAG_BATCHER , "%s: invoking processor %s with %d items" , this , processor , toProcess . size ( ) ) ; processor . process ( toProcess ) ; } else Log . v ( Log . TAG_BATCHER , "%s: nothing to process" , this ) ; synchronized ( mutex ) { lastProcessedTime = System . currentTimeMillis ( ) ; scheduled = false ; scheduleBatchProcess ( scheduleNextBatchImmediately ) ; Log . v ( Log . TAG_BATCHER , "%s: invoking processor done" , this , processor , toProcess . size ( ) ) ; } processMutex . notifyAll ( ) ; } }
This method is called by the work executor to do the batch process . The inbox items up to the batcher capacity will be taken out to process . The next batch will be rescheduled if there are still some items left in the inbox .