I was looking inside the AtomicInteger Class and I came across the following method:
/**
* Atomically increments by one the current value.
*
* @return the previous value
*/
public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}
Can someone explain what for(;;) means?
解决方案
It is equivalent to while(true).
A for-loop has three elements:
initializer
condition (or termination expression)
increment expression
for(;;) is not setting any of them, making it an endless loop.