I have following kind of scenario: It is simple form of code that I am showing here to clarify my concern:
public void callMe()
{
AtomicInteger howManyOdds = new AtomicInteger(0);
AtomicInteger howManyEvens = new AtomicInteger(0);
loopThrough(100,howManyOdds,howManyEvens);
System.out.println(howManyOdds.get()+" "+howManyEvens.get());
}
private void loopThrough(int counter,AtomicInteger howManyOdds,AtomicInteger howManyEvens)
{
for(int i = 1 ; i <= counter ;i++)
{
if(i%2 == 0)
howManyEvens.getAndAdd(1);
else
howManyOdds.getAndAdd(1);
}
}
I know that it can be done by int[]
but it looks kind of odd. Is AtomicInteger a good substitute for Mutable Integer in such kind of cases? If No then WHY?