What is the advantage of using the class Unsafe to write atomics over synchronize blocks?
why java Atomics uses sun Unsafe class rather than usi开发者_开发知识库ng synchronize block/volatile?
synchronization is much more heavy weight.
The backport of the concurrency library for Java 1.4 uses synchronization however it doesn't perform any where near as well.
Unsafe gives direct access to the Compare-and-Swap instructions of the CPU.
I can think of that the Programmers of the Atomics classes know what they do so they use the low level methodes for better performance.
Synchronize is a really heavy tool when doing multithreaded operations. Its way to powerful/blooted for simple locking/mutual exclusion.
精彩评论