Home > OS >  What is the difference between Compare and Swap and Compare and Set?
What is the difference between Compare and Swap and Compare and Set?

Time:09-21

What is the difference between compareAndSet() in java.util.concurrent.atomic.AtomicInteger and compare and swap (CAS)? Are they the same with different implementations?

CodePudding user response:

  • They are not the same. Compare and swap is a hardware build atomic instruction that compares a given parameter with an existing variable and replace its value with another given variable to return a boolean value depending on the successfulness of the replacement.
  • The boolean value returned from the above function is generally called compare and set.
  • Also, java implementation of compare and swap atomic instruction is called compareAndSet().

I guess I answered your question.

  • Related