我正在查看AtomicInteger类内部,我遇到了以下方法:
/**
* 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;
}
}
Run Code Online (Sandbox Code Playgroud)
有人解释是什么for(;;)意思吗?
这是一样的
while(true) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
......只是不太清楚.
请注意,如果compareAndSet(current, next)将评估为,则循环将退出true.