原子包类如AtomicInteger等如何工作 [英] How do the atomic package classes as AtomicInteger etc work

查看:77
本文介绍了原子包类如AtomicInteger等如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过使用原子包类使我们能够做到线程安全,锁定更少的编码。
但是我不太确定原子包类中的方法如何在没有使用锁或任何synchronize关键字的情况下提供线程安全性。任何帮助都应该受到赞赏。

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.

推荐答案

他们使用非常低级别的指令,例如比较和交换,以及 sun.misc.Unsafe class。

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

基本上,像 compareAndSwap()之类的方法调用将会对应于唯一的处理器指令,它消除了许多多线程问题。

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

这篇关于原子包类如AtomicInteger等如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆