I have this code in an ejb
for (PayrollEntry pe : payroll.getEntries()) {
recalculatePayrollEntry(pe);
}
CalculateTotals(payroll);
which calls this async method
@Asynchronous
public void recalculatePayrollEntry(PayrollEntry pe) {
// Calculate Payroll Entry;
pe.setEarningsEntries(newEarnings);
}
What is the best way to wait until all those recalculations execute before calling CalcuateTotals?
Instead of having your async method as
void, return a Future (which represent an asynchronous calculation). Start up the jobs, collect all the futures, and then await their completion: