I do not think this is a good idea: using AtomicInteger
in contexts that are not inherently concurrent is misleading to the reader.
Using an array is not a good idea, either, even though technically it works well. The problem is that the resultant code is not too descriptive, in the sense that the "mapping" of indexes to their meanings (i.e. 0
-> odd, 1
-> even) is not visible from the API itself.
You would be better off with a mutable class that holds two properties:
public class OddEven {
int odd, even;
public int getOdd() {return odd;}
public int getEven() {return even;}
public void incOdd() {odd++;}
public void incEven() {even++;}
}
This achieves a very good readability, without creating a false impression that something concurrent is going on behind the scene.