java - Is it good practice to use AtomicInteger as a substitute for mutable Integer? -


I have the following scenario: This is a simple form of code that I'm showing here to explain my concern I: Public Zero Columns () {Atomic integer howohonobbs = NewElectricIntergrader (0); AtomicInteger howManyEvens = New AtomicInteger (0); loopthrough (100, howManyOdds, howManyEvens); System.out.println (howManyOdds.get () + "" + howManyEvens.get ()); } Private Zero Loopthu (Int counter, AtomicIntergeor howManyOdds, AtomicInteger howManyEvens) {for (int i = 1; i & lt; = counter; i ++) {if (M = 2 == 0) howManyEvens.get and Add (1) ; And how many people are there? Add and Add (1); }}

I know that this can be done by int [] but it looks strange. Is it a good option for a variable integer in atomic integers in such cases? If not then why?

I do not think this is a good idea: in reference AtomicInteger < Using / code> is not naturally concurrent, the reader is misleading.

Using an array is not a good idea, even if it works technically well. The problem is that the resultant code is also not descriptive, in that sense the "mapping" of the indicators means its meaning (i.e. 0 -> strange, 1 -> gt; Even though) is not visible from the API itself.

You will be better off with a multiplier class, which holds two properties:

  public squares oddven {int strange, even; Public int getOdd () {odd return}} public int getEven () {return even;} public zero incOdd () {strange ++;} public zero incEven () {even ++;}}   

It receives a very good readability, creating a misconception that there is some concurrent running behind the scene.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -