java - How to check another condition before set AtomicBoolean? -


i have atomicboolean , want set it, if condition true:

for example have bankaccount active , saldo field. in method setinctive, have check if saldo field 0 , set active field false.

this solution, i'm not sure if works!

private final atomicboolean active = new atomicboolean(true); private final atomicreference<double> saldo = new atomicreference<>(0.0);  @override public boolean setinctive() {     //first: check if saldo 0.0     //second: set active false      saldo.getandupdate(adouble ->  {         if(adouble == 0)             active.set(false);          return adouble;     });      //return negated value of active field     return !active.get(); } 

what correct solution atomics , possible without using locks?

one possible way , check condition , set boolean, use atomicmarkablereference. im example of type double:

private final atomicmarkablereference<double> saldo = new atomicmarkablereference<>(0.0, true);  @override public boolean setinctive() {     return saldo.attemptmark(0.0, false); } 

the saldo field represents not current saldo, status of bank account.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -