MENU

Fun & Interesting

Compare and Swap in Java

Jakob Jenkov 19,380 lượt xem 3 years ago
Video Not Working? Fix It Now

Compare and swap is an alternative to Java synchronized blocks when designing concurrent data structures or algorithms. A compare and swap operation first compares the value of a variable with an expected value, and if the values are equal, swaps the value of the variable with a new value. Compare and swap is abbreviated CAS. A compare and swap operation is atomic.

Compare and swap is especially useful for check-then-act type code. You can use compare and swap operations to guard a critical section, or for optimisticl locking style concurrency.

Chapters:
0:00 Compare and swap introduction
0:31 Compare and swap example
1:45 Compare and swap for check-then-act cases
5:45 Compare and swap used in a lock implementation
9:32 Compare and swap vs. Java synchronized blocks
17:59 Compare and swap as guard for critical section
18:59 Compare and swap for optimistic locking

Compare and swap tutorial - text:
http://tutorials.jenkov.com/java-concurrency/compare-and-swap.html

Java Concurrency tutorial - text / video:
http://tutorials.jenkov.com/java-concurrency/index.html
https://www.youtube.com/playlist?list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4

Comment