java-memory-model

Why is `synchronized (new Object()) {}` a no-op?

耗尽温柔 提交于 2019-12-04 14:56:45
问题 In the following code: class A { private int number; public void a() { number = 5; } public void b() { while(number == 0) { // ... } } } If method b is called and then a new thread is started which fires method a, then method b is not guaranteed to ever see the change of number and thus b may never terminate. Of course we could make number volatile to resolve this. However for academic reasons let's assume that volatile is not an option: The JSR-133 FAQs tells us: After we exit a synchronized

Partial constructed objects in the Java Memory Model

这一生的挚爱 提交于 2019-12-04 14:47:45
I came across the following code in an article somewhere on the Internet : public class MyInt { private int x; public MyInt(int y) { this.x = y; } public int getValue() { return this.x; } } The article states that Constructors are not treated special by the compiler (JIT, CPU etc) so it is allowed to reorder instructions from the constructor and instructions that come after the constructor. Also, this JSR-133 article about the Java Memory Model states that A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly

Is it possible to efficiently implement a seqlock in Java?

妖精的绣舞 提交于 2019-12-04 09:30:25
Another question made me wonder if the seqlock can be efficiently implemented with a volatile version counter in Java. Here's a prototypical implementation, for the case there will only ever be a single writer thread: class Seqlock { private volatile long version = 0; private final byte[] data = new byte[10]; void write(byte[] newData) { version++; // 1 System.arraycopy(newData, 0, data, 0, data.length); // 2 version++; // 3 } byte[] read() { long v1, v2; byte[] ret = new byte[data.length]; do { v1 = version; // 4 System.arraycopy(data, 0, ret, 0, data.length); // 5 v2 = version; // 6 } while

Is setting a HashMap thread safe?

◇◆丶佛笑我妖孽 提交于 2019-12-04 02:07:53
I have a HashMap in my program which is accessed by multiple threads, and is occasionally set by a single thread. For example: Map<String, String> myMap = new HashMap<String, String>(); This is accessed by multiple threads. Once an hour, a single thread calls: myMap = myRefreshedVersionOfTheMap; So my question is whether or not this is thread safe. If both maps always have the key "importantKey" , is it possible for a reading thread to ever access the map at a time when "importantKey" does not exist? Edit: Thanks to the answers, I've realized this question is actually independent of the

Value integrity guarantee for concurrent long writes in 64-bit OpenJDK 7/8

一曲冷凌霜 提交于 2019-12-03 12:28:33
Note: this question isn't related to volatile, AtomicLong, or any perceived deficiency in the described use case. The property I am trying to prove or rule out is as follows: Given the following: a recent 64-bit OpenJDK 7/8 (preferably 7, but 8 also helpful) a multiprocessing Intel-base system a non-volatile long primitive variable multiple unsynchronized mutator threads an unsynchronized observer thread Is the observer always guaranteed to encounter intact values as written by a mutator thread, or is word tearing a danger? JLS: Inconclusive This property exists for 32-bit primitives and 64

Why is `synchronized (new Object()) {}` a no-op?

北慕城南 提交于 2019-12-03 09:17:54
In the following code: class A { private int number; public void a() { number = 5; } public void b() { while(number == 0) { // ... } } } If method b is called and then a new thread is started which fires method a, then method b is not guaranteed to ever see the change of number and thus b may never terminate. Of course we could make number volatile to resolve this. However for academic reasons let's assume that volatile is not an option: The JSR-133 FAQs tells us: After we exit a synchronized block, we release the monitor, which has the effect of flushing the cache to main memory , so that

final vs volatile guaranntee w.rt to safe publication of objects

此生再无相见时 提交于 2019-12-03 08:52:26
问题 From the book Java concurrency in practice : To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A properly constructed object can be safely published by: Initializing an object reference from a static initializer Storing a reference to it into a volatile field or AtomicReference Storing a reference to it into a final field of a properly constructed object Storing a reference to it into a field that is

Java - Immutable array thread-safety

混江龙づ霸主 提交于 2019-12-03 06:21:36
I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that other threads will see the most recent version of data stored in the array after construction. Of

Effectively Immutable Object

坚强是说给别人听的谎言 提交于 2019-12-03 04:34:52
问题 I want to make sure that I correctly understand the 'Effectively Immutable Objects' behavior according to Java Memory Model. Let's say we have a mutable class which we want to publish as an effectively immutable: class Outworld { // This MAY be accessed by multiple threads public static volatile MutableLong published; } // This class is mutable class MutableLong { private long value; public MutableLong(long value) { this.value = value; } public void increment() { value++; } public long get()

Could the JIT collapse two volatile reads as one in certain expressions?

◇◆丶佛笑我妖孽 提交于 2019-12-03 03:39:43
问题 Suppose we have a volatile int a . One thread does while (true) { a = 1; a = 0; } and another thread does while (true) { System.out.println(a+a); } Now, would it be illegal for a JIT compiler to emit assembly corresponding to 2*a instead of a+a ? On one hand the very purpose of a volatile read is that it should always be fresh from memory. On the other hand, there's no synchronization point between the two reads, so I can't see that it would be illegal to treat a+a atomically, in which case I