Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the atomic package classes as AtomicInteger etc work

I have read that using atomic package classes enables us to do thread safe,lock less coding. But I am not very sure about how do the methods in the classes of atomic package provide thread safety in absence of use of locks or any synchronize keyword.Any help shall be appreciated.

like image 722
user1649415 Avatar asked Sep 06 '12 16:09

user1649415


1 Answers

They use very low level instructions such as Compare and Swap, and multiple other methods from sun.misc.Unsafe class.

Basically, a method call like compareAndSwap() will correspond to a unique processor instruction, which remove a lot of multithreading issues.

like image 102
jolivier Avatar answered Oct 20 '22 00:10

jolivier