java-memory-model

Java Concurrency - Publishing Immutable Objects (Java Concurrency In Practice)

血红的双手。 提交于 2019-12-01 01:26:33
In Java Concurrency In Practice, the author stated that Immutable objects can be published through any mechanism Immutable objects can be used safely by any thread without additional synchronization, even when synchronization is not used to publish them. Does it mean that the following idioms are safe to publish immutable objects? public static List<ImmutableObject> list = new ArrayList<ImmutableObject>(); // thread A invokes this method first public static void methodA () { list.add(new ImmutableObject()); } // thread B invokes this method later public static ImmutableObject methodB () {

How can an immutable object, with non-final fields, be thread unsafe?

浪尽此生 提交于 2019-11-30 13:42:55
say we have this // This is trivially immutable. public class Foo { private String bar; public Foo(String bar) { this.bar = bar; } public String getBar() { return bar; } } What makes this thread unsafe ? Following on from this question . Foo is thread safe once it has been safely published. For example, this program could print "unsafe" (it probably won't using a combination of hotspot/x86) - if you make bar final it can't happen: public class UnsafePublication { static Foo foo; public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { while (foo ==

If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?

安稳与你 提交于 2019-11-30 10:13:14
I've looked at this answer , and it states how: Under the new memory model, when thread A writes to a volatile variable V, and thread B reads from V, any variable values that were visible to A at the time that V was written are guaranteed now to be visible to B. Therefore, given the example: public class Main { static int value = -1; static volatile boolean read; public static void main(String[] args) { Thread a = new Thread(() -> { value = 1; read = true; }); Thread b = new Thread(() -> { while (!read); System.out.println("Value: " + value); }); a.start(); b.start(); } } Is the change to

Are assignments to non-volatile member variables in one thread guaranteed to be seen in another thread?

南笙酒味 提交于 2019-11-30 04:43:58
问题 Consider the Java example below. Notice that neither of the class member variables are declared to be volatile . If I am understanding the memory model and "happens before" rules correctly, a Java implementation could optimize the run() method so that it runs forever, even if another thread calls the stopNow() method. This can happen because there is nothing in the run() method that forces the thread to read the value of stop more than once. Is that correct? If not, why not? class Example

Interpretation of “program order rule” in Java concurrency

自闭症网瘾萝莉.ら 提交于 2019-11-29 23:17:50
Program order rule states "Each action in a thread happens-before every action in that thread that comes later in the program order" 1.I read in another thread that an action is reads and writes to variables locks and unlocks of monitors starting and joining with threads Does this mean that reads and writes can be changed in order, but reads and writes cannot change order with actions specified in 2nd or 3rd lines? 2.What does "program order" mean? Explanation with an examples would be really helpful. Additional related question Suppose I have the following code: long tick = System.nanoTime();

If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?

拈花ヽ惹草 提交于 2019-11-29 15:55:05
问题 I've looked at this answer, and it states how: Under the new memory model, when thread A writes to a volatile variable V, and thread B reads from V, any variable values that were visible to A at the time that V was written are guaranteed now to be visible to B. Therefore, given the example: public class Main { static int value = -1; static volatile boolean read; public static void main(String[] args) { Thread a = new Thread(() -> { value = 1; read = true; }); Thread b = new Thread(() -> {

Why setArray() method call required in CopyOnWriteArrayList

这一生的挚爱 提交于 2019-11-29 01:16:55
问题 In CopyOnWriteArrayList.java, in the method set(int index, E element) below public E set(int index, E element) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); Object oldValue = elements[index]; if (oldValue != element) { int len = elements.length; Object[] newElements = Arrays.copyOf(elements, len); newElements[index] = element; setArray(newElements); } else { // Not quite a no-op; ensures volatile write semantics setArray(elements);----? Why this

Is volatile read happens-before volatile write?

大城市里の小女人 提交于 2019-11-28 23:34:55
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? No, a volatile read before (in synchronization order) a volatile write of the same variable does not necessarily happens-before the volatile write. This means they can be in a "data race", because they are "conflicting accesses not

Interpretation of “program order rule” in Java concurrency

余生长醉 提交于 2019-11-28 20:37:00
问题 Program order rule states "Each action in a thread happens-before every action in that thread that comes later in the program order" 1.I read in another thread that an action is reads and writes to variables locks and unlocks of monitors starting and joining with threads Does this mean that reads and writes can be changed in order, but reads and writes cannot change order with actions specified in 2nd or 3rd lines? 2.What does "program order" mean? Explanation with an examples would be really

Please explain initialization safety as spelled out in Java memory model

白昼怎懂夜的黑 提交于 2019-11-28 19:33:36
Can some one explain initialization safety as required by Java memory model ? How does the final fields help in achieving initialization safety ? What role does the constructor play in ensuring initialization safety ? Initialization safety provides for an object to be seen by an external thread in its fully constructed ( initialized ) state. The prerequisite is that the object should not be published prematurely ie. in its constructor. Once this is ensured , JMM requires certain behaviour for the fields that are declared as final . First , all final object fields are guarenteed to be seen by