java-memory-model

What are the similarities between the Java memory model and the C++11 memory model?

不羁的心 提交于 2019-12-03 02:12:57
问题 The new c++ standard introduces the notion of a memory model. There were already questions on SO about it, what does it mean, how does it change the way we write code in c++ and so on. I'm interested in getting to know how does the C++ memory model relate to the older, well known java memory model (1.5). Is it the same? Is it similar? Do they have any significant differences? If so, why? The java memory model has been around since a long time and many people know it quite decently, so I guess

Dalvik VM & Java Memory Model (Concurrent programming on Android)

狂风中的少年 提交于 2019-12-03 01:19:30
问题 I am working on Android projects which involve the lot of concurrent programming and I am going to implement some custom inter-threads communication stuff (the one from java.util.concurent are not well suited for my purposes). The concurrent programming is not easy in general but with Dalvik it seems to be even harder. To get the correct code you should know some specific things and that where problem arise with Dalvik. I just can't find a detailed documentation about the Dalvik VM. Most

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

匆匆过客 提交于 2019-12-02 22:47:15
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 properly guarded by a lock. My questions are : What are the the differences between bullet points 2 and 3

Effectively Immutable Object

你说的曾经没有我的故事 提交于 2019-12-02 18:53:16
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() { return value; } } We do the following: // Create a mutable object and modify it MutableLong val = new

What are the similarities between the Java memory model and the C++11 memory model?

寵の児 提交于 2019-12-02 17:16:11
The new c++ standard introduces the notion of a memory model. There were already questions on SO about it, what does it mean, how does it change the way we write code in c++ and so on. I'm interested in getting to know how does the C++ memory model relate to the older, well known java memory model (1.5). Is it the same? Is it similar? Do they have any significant differences? If so, why? The java memory model has been around since a long time and many people know it quite decently, so I guess it might be helpful, not only for me, to learn the C++ memory model, by comparing it with the java one

Dalvik VM & Java Memory Model (Concurrent programming on Android)

我与影子孤独终老i 提交于 2019-12-02 14:36:55
I am working on Android projects which involve the lot of concurrent programming and I am going to implement some custom inter-threads communication stuff (the one from java.util.concurent are not well suited for my purposes). The concurrent programming is not easy in general but with Dalvik it seems to be even harder. To get the correct code you should know some specific things and that where problem arise with Dalvik. I just can't find a detailed documentation about the Dalvik VM. Most Android resources (even the developer.android.com focused on platform API and doesn't provide any deep

Does JVM guarantee to cache not volatile variable?

六月ゝ 毕业季﹏ 提交于 2019-12-02 12:50:28
问题 Does JVM guarantee to cache not volatile variable ? Can a programer depend upon on JVM to always cache non-volatile variables locally for each thread. Or JVM may or may not do this, thus a programer should not depend upon JVM for this. Thanks for the answers in advance. 回答1: No. The JVM doesn't guarantee "caching" of non-volatile fields. What implementations of JVM guarantee is how volatile fields should behave . Caching of fields is non-standard (unspecified) and can vary from JVM to JVM

Does JVM guarantee to cache not volatile variable?

牧云@^-^@ 提交于 2019-12-02 04:34:44
Does JVM guarantee to cache not volatile variable ? Can a programer depend upon on JVM to always cache non-volatile variables locally for each thread. Or JVM may or may not do this, thus a programer should not depend upon JVM for this. Thanks for the answers in advance. No. The JVM doesn't guarantee "caching" of non-volatile fields. What implementations of JVM guarantee is how volatile fields should behave . Caching of fields is non-standard (unspecified) and can vary from JVM to JVM implementation. So, you shouldn't really rely on it (even if find out, by some way that some data is being

Happens-before rules in Java Memory Model

旧时模样 提交于 2019-12-01 17:53:00
I am currently studying for a concurrent programming exam and don't understand why the output of this program is 43. Why is x = y + 1 executed before t.start() ? I also should explain which happens-before rules I used. If I understand the program order rule (each action in a thread happens-before every action in that thread that comes later in the program order) t.start() has to be executed before x = y + 1 so that thread t copies variable x which will be 1. public class HappensBefore { static int x = 0; static int y = 42; public static void main(String[] args) { x = 1; Thread t = new Thread()

Happens-before rules in Java Memory Model

前提是你 提交于 2019-12-01 17:32:55
问题 I am currently studying for a concurrent programming exam and don't understand why the output of this program is 43. Why is x = y + 1 executed before t.start() ? I also should explain which happens-before rules I used. If I understand the program order rule (each action in a thread happens-before every action in that thread that comes later in the program order) t.start() has to be executed before x = y + 1 so that thread t copies variable x which will be 1. public class HappensBefore {