Leave a Reply 3

Your email address will not be published. Required fields are marked *


Bahaa Khateib

Bahaa Khateib

The answer is 00000 new Line 11111 , please make sure that in the first loop the var[i]=new AtomicInteger(); not AtomicInteger(i);

Humberto Bañuelos Flores

Humberto Bañuelos Flores

/**
*
* @author Humberto
*/
import java.util.concurrent.atomic.AtomicInteger;

public class Incrementor {

public static void main(String[] args) {
AtomicInteger[] var = new AtomicInteger[5];
for (int i = 0; i < 5; i++) {
var[i] = new AtomicInteger();
}
for (int i = 0; i < var.length; i++) {
var[i].incrementAndGet();
if (i == 2) {
var[i].compareAndSet(1, 4);
}
System.out.print(var[i] + " ");
}
}
}
1 1 4 1 1

gelete

gelete

A ?

Output:
0 0 0 0 0
1 1 1 1 1