How to make sure that these two concurrent data structure statements get executed as an atomic block without using synchronized
statement
so that the sum is consistent at any given point in time?
Note : I am new to multi-threading in java.
AtomicLong sumOfAllItems=new AtomicLong();
AtomicLong itemSpecificSum=new AtomicLong();
public void addPrice(long price){
// how to make sure that these two statements get executed
// with synchronised() block
sumOfAllItems.addAndGet(price);
itemSpecificSum.addAndGet(price);
}