I have spent few hours today to understand this as this was not clear from documentation. Also, the test seems to say that my understanding is wrong. So, wanted to verify with the community.
So, Questions is: Lets say you have 1000 records, and a batch to process these records. Default batch size is 200. So, this means there will be 5 time execute method will be invoked. But, will they be invoked one after other or in parallel.
I tested it with 16 contact records, & executed batch with size 1. Serial is a custom field to order:
Batch class:
public class DemoBatch implements Database.Batchable<sObject>, Database.stateful {
public String a='';
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([select id, firstName,serial__c from contact order by serial__c]);
}
public void execute(Database.BatchableContext BC, List<Contact> scope){
System.debug('***scope'+scope[0].serial__c);
if(scope[0].serial__c==6){
for(Integer i = 0; i < 10000000;i++){
}
if(scope[0].serial__c==4){
System.debug('***long');
for(Integer i = 0; i < 10000000;i++){
}
}
}
a = a + scope[0].serial__c;
}
public void finish(Database.BatchableContext BC){
System.debug('***a:Finish:'+a);
}
}
Though, I have intentially added delays here, the end result is always ordered. So, are individual executions are serial ??