java - whats the purpose of having infinite for loop in following code -



java - whats the purpose of having infinite for loop in following code -

can explain ....this official java atomicboolean getandset method's definition

public final boolean getandset(boolean newvalue) { (;;) { boolean current = get(); if (compareandset(current, newvalue)) homecoming current; } }

in java 8, sourcecode has been restructured, making easier understand:

public final boolean getandset(boolean newvalue) { boolean prev; { prev = get(); } while (!compareandset(prev, newvalue)); homecoming prev; }

as can see, compareandset, returns boolean, comes native function unsafe.compareandswapint, might fail. in case, operation repeated.

according documentation of unsafe.compareandswapint,

atomically update java variable x if holding expected. returns: true if successful

the function fail if value of atomicboolean has been changed between calling get() , point in unsafe.compareandswapint. shouldn't case, when happens, poll current value 1 time 1 time again , hope same thing doesn't repeat.

java

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -