volatile

Java, volatile and memory barriers on x86 architecture

 ̄綄美尐妖づ 提交于 2021-01-02 07:18:19
问题 This is more of a theoretical question. I'm not sure if all concepts, compiler behaviors, etc. are uptodate and still in use, but I'd like to have confirmation if I'm correctly understanding some concepts I'm trying to learn. Language is Java. From what I've understood so far, on X86 architecture, StoreLoad barriers (despite the exact CPU instructions used to implement them) are put after Volatile writes, to make them visible to subsequent Volatile Reads in other threads (since x86 doesn't

does volatile keword in java really have to do with caches?

一世执手 提交于 2020-12-29 08:44:51
问题 From what I've read, the "volatile" keyword in java ensures that a thread always fetches the most up-to-date value of a particular pointer, usually by reading/writing directly from/to memory to avoid cache inconsistencies. But why is this needed? To my knowledge, this is already done on a hardware level. If I remember correctly from my system architecture class, A processor-core that updates a memory location, sends an invalidation signal to the other processor's caches, forcing them to fetch

does volatile keword in java really have to do with caches?

非 Y 不嫁゛ 提交于 2020-12-29 08:43:07
问题 From what I've read, the "volatile" keyword in java ensures that a thread always fetches the most up-to-date value of a particular pointer, usually by reading/writing directly from/to memory to avoid cache inconsistencies. But why is this needed? To my knowledge, this is already done on a hardware level. If I remember correctly from my system architecture class, A processor-core that updates a memory location, sends an invalidation signal to the other processor's caches, forcing them to fetch

does volatile keword in java really have to do with caches?

こ雲淡風輕ζ 提交于 2020-12-29 08:42:54
问题 From what I've read, the "volatile" keyword in java ensures that a thread always fetches the most up-to-date value of a particular pointer, usually by reading/writing directly from/to memory to avoid cache inconsistencies. But why is this needed? To my knowledge, this is already done on a hardware level. If I remember correctly from my system architecture class, A processor-core that updates a memory location, sends an invalidation signal to the other processor's caches, forcing them to fetch

java并发编程

我的未来我决定 提交于 2020-11-22 13:47:49
一、 volatile 基本介绍 Java 语言提供了一种稍弱的同步机制,即 volatile 变量.用来确保将变量的更新操作通知到其他线程,保证了新值能立即同步到主内存,以及每次使用前立即从主内存刷新. 当把变量声明为volatile类型后,编译器与运行时都会注意到这个变量是共享的. volatile 变量对所有线程是立即可见的,对 volatile 变量所有的写操作都能立即反应到 其他线程之中,换句话说:volatile 变量在各个线程中是一致的。 但是volatile并不是线程安全的。 volatile关键字能保证可见性,可见性只能保证每次读取的是最新的值;但是voliate没法保证对变量的操作的原子性。 二、举例说明 /** * * @author Yuanyuan */ public class Counter { public volatile int inc = 0; //一个线程执行increase的时候能保证得到了最新的当前值,但还没有执行自增操作,该线程就有可能被阻塞,此时其他线程也得到了这个最新值,执行了自增操作,这样之前那个阻塞的线程执行时就出错了。 public void increase() { System.out.println(inc); inc++; } public static void main(String[] args) { final