I need to perform the following operation:
// average, total, elapsed are Long's
average = ( ( total * average ) + elapsed ) / (++total);
But I want to use AtomicLong
This is what I'm trying but I don't quite get if it is correct:
average.set( (( total.get() * average.get() ) + elapsed) / total.getAndIncrement() );
How can I tell if this is correct?
In your assignment total might be different in the first total and total++. You need to synchronize the whole operation.