java-memory-model

Is volatile read happens-before volatile write?

你离开我真会死。 提交于 2019-11-27 15:04:12
问题 I try to understand why this example is a correctly synchronized program: a - volatile Thread1: x=a Thread2: a=5 Because there are conflicting accesses (there is a write to and read of a) so in every sequential consistency execution must be happens-before relation between that accesses. Suppose one of sequential execution: 1. x=a 2. a=5 Is 1 happens-before 2, why? 回答1: No, a volatile read before (in synchronization order) a volatile write of the same variable does not necessarily happens

Double-checked locking without volatile

非 Y 不嫁゛ 提交于 2019-11-27 10:51:11
问题 I read this question about how to do Double-checked locking: // Double-check idiom for lazy initialization of instance fields private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null) // Second check (with locking) field = result = computeFieldValue(); } } return result; } My aim is to get lazy-loading a field (NOT a singleton) work without the volatile attribute

Why does this Java program terminate despite that apparently it shouldn't (and didn't)?

江枫思渺然 提交于 2019-11-27 09:57:45
A sensitive operation in my lab today went completely wrong. An actuator on an electron microscope went over its boundary, and after a chain of events I lost $12 million of equipment. I've narrowed down over 40K lines in the faulty module to this: import java.util.*; class A { static Point currentPos = new Point(1,2); static class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } } public static void main(String[] args) { new Thread() { void f(Point p) { synchronized(this) {} if (p.x+1 != p.y) { System.out.println(p.x+" "+p.y); System.exit(1); } } @Override public void run(

Detailed semantics of volatile regarding timeliness of visibility

假如想象 提交于 2019-11-27 06:45:55
Consider a volatile int sharedVar . We know that the JLS gives us the following guarantees: every action of a writing thread w preceding its write of value i to sharedVar in program order happens-before the write action; the write of value i by w happens-before the successful read of i from sharedVar by a reading thread r ; the successful read of i from sharedVar by the reading thread r happens-before all subsequent actions of r in program order. However, there is still no wall-clock time guarantee given as to when the reading thread will observe the value i . An implementation that simply

How can CopyOnWriteArrayList be thread-safe?

一世执手 提交于 2019-11-27 06:11:20
I've taken a look into OpenJDK source code of CopyOnWriteArrayList and it seems that all write operations are protected by the same lock and read operations are not protected at all. As I understand, under JMM all accesses to a variable (both read and write) should be protected by lock or reordering effects may occur. For example, set(int, E) method contains these lines (under lock): /* 1 */ int len = elements.length; /* 2 */ Object[] newElements = Arrays.copyOf(elements, len); /* 3 */ newElements[index] = element; /* 4 */ setArray(newElements); The get(int) method, on the other hand, only

Volatile guarantees and out-of-order execution [duplicate]

ぃ、小莉子 提交于 2019-11-27 02:59:41
This question already has an answer here: Java memory model: volatile variables and happens-before 3 answers IMPORTANT EDIT I know about the "happens before" in the thread where the two assignments are happening my question is would it be possible for another thread to be reading "b" non-null while "a" is still null. So I know that if you're calling doIt() from the same thread as the one where you previously called setBothNonNull(...) then it cannot throw a NullPointerException. But what if one is calling doIt() from another thread than the one calling setBothNonNull(...) ? Note that this

What is the scope of memory flushed or published to various threads when using volatile and synchronized?

瘦欲@ 提交于 2019-11-27 02:21:02
问题 This question is in reference to memory visibility only, not happens-before and happens-after. There are four ways in Java that guarantees changes to memory in one thread to be made visible to another thread. (reference http://gee.cs.oswego.edu/dl/cpj/jmm.html) A writing thread releases a synchronization lock and a reading thread subsequently acquires that same synchronization lock. If a field is declared as volatile, any value written to it is flushed and made visible by the writer thread

Are hoisting and reordering the same thing?

こ雲淡風輕ζ 提交于 2019-11-27 01:55:35
问题 I read from Effective Java that In the absence of synchronization the following sequence A below can be converted into sequence B by the virtual machine and this is called hoisting . I also read somewhere that if variables are not declared as volatile instructions involving the variables can be reordered . Are hoisting and reordering the same thing? while (!done) sequence A i++; if (!done) while (true) sequence B i++; 回答1: They are slightly different. Hoisting means that you have pulled some

Java concurrency: is final field (initialized in constructor) thread-safe?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 01:51:27
Can anyone tell me whether this class is threadsafe or not ? class Foo { private final Map<String,String> aMap; public Foo() { aMap = new HashMap<String, String>(); aMap.put("1", "a"); aMap.put("2", "b"); aMap.put("3", "c"); } public String get(String key) { return aMap.get(key); } } Edit: It my fault to not clarify the question. According to JMM FAQ : A new guarantee of initialization safety should be provided. If an object is properly constructed (which means that references to it do not escape during construction), then all threads which see a reference to that object will also see the

What does “volatile” mean in Java?

怎甘沉沦 提交于 2019-11-27 01:49:04
We use volatile in one of our projects to maintain the same copy of variable accessed by different threads. My question is whether it is alright to use volatile with static . The compiler does not give any errors but I don't understand the reason of using both. ide Short of reading the memory model specification, I recommend you read http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html . It's written by one of the JMM authors and should answer your question. Thinking of memory reads and writes in terms of the happens-before clause is also helpful; the JMM for Java 5