Java 5,引入了很少的并发原语,如比较和交换、比较和设置(合并为CAS )和其他一些。
据我所知,所有这些操作都保证是原子的。因此,似乎每个操作都必须有一个JVM字节码指令?
我正在查看字节码指令列表,但没有找到任何用于CAS之类的方法。
不确定,我说得对,CAS必须有单个字节码指令,还是用java执行/实现CAS机构的方式不同?
发布于 2013-12-26 08:47:00
从Java操作看起来像是对类sun.misc.Unsafe对象的本机方法调用。一个来自AtomicInteger的例子
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
*
* @param expect the expected value
* @param update the new value
* @return true if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}更多关于不安全的信息:https://stackoverflow.com/questions/5574241/interesting-uses-of-sun-misc-unsafe。
发布于 2013-12-26 08:02:19
因此,似乎每个操作都必须有一个JVM字节码指令?
实际上,这些操作是作为本机代码方法实现的,这些方法使用特定于硬件的指令或指令序列来实现所需的语义。没有用于执行CAS操作的JVM字节码。
https://stackoverflow.com/questions/20781178
复制相似问题