java-memory-model

Is Dalvik's memory model the same as Java's?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 00:06:25
Is Dalvik's memory model the same as Java's ? I am particularly interested in whether reads and writes of reference and non- long /non- double primitive variables are atomic, but I would also like to know whether there are any differences between the two platforms' memory models. As of 4.0 (Ice Cream Sandwich), Dalvik's behavior should match up with JSR-133 (the Java Memory Model). As of 3.0 (Honeycomb), most of the pieces were in place, but some minor things had been overlooked that would be difficult to encounter in practice (e.g. some edge cases in finalization). As of 2.3 (Gingerbread),

Immutability and reordering

北城以北 提交于 2019-11-26 18:56:49
问题 The code below (Java Concurrency in Practice listing 16.3) is not thread safe for obvious reasons: public class UnsafeLazyInitialization { private static Resource resource; public static Resource getInstance() { if (resource == null) resource = new Resource(); // unsafe publication return resource; } } However, a few pages later, in section 16.3, they state: UnsafeLazyInitialization is actually safe if Resource is immutable. I don't understand that statement: If Resource is immutable, any

Instruction reordering & happens-before relationship in java [duplicate]

痴心易碎 提交于 2019-11-26 18:46:57
问题 This question already has an answer here: How to understand happens-before consistent 4 answers In the book Java Concurrency In Practice, we are told several time that the instructions of our program can be reordered, either by the compiler, by the JVM at runtime, or even by the processor. So we should assume that the executed program will not have its instructions executed in exactly the same order than what we specified in the source code. However, the last chapter discussing Java Memory

Must all properties of an immutable object be final?

左心房为你撑大大i 提交于 2019-11-26 15:19:24
Must immutable objects have all properties be final ? According to me not. But I don't know, whether I am right. assylias The main difference between an immutable object (all properties final) and an effectively immutable object (properties aren't final but can't be changed) is safe publication. You can safely publish an immutable object in a multi threaded context without having to worry about adding synchronization, thanks to the guarantees provided by the Java Memory Model for final fields : final fields also allow programmers to implement thread-safe immutable objects without

Memory effects of synchronization in Java

随声附和 提交于 2019-11-26 15:10:59
问题 JSR-133 FAQ says: But there is more to synchronization than mutual exclusion. Synchronization ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which synchronize on the same monitor. After we exit a synchronized block, we release the monitor, which has the effect of flushing the cache to main memory, so that writes made by this thread can be visible to other threads. Before we can enter a synchronized block,

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

前提是你 提交于 2019-11-26 14:58:15
问题 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)

What is in java object header

爷,独闯天下 提交于 2019-11-26 13:06:42
Could you give me some information on what is exactly stored in object header? I know, that it's probably JVM dependent, but maybe for HotSpot at least? I'm looking for exact description specifically for a first row. I've read several information that I can't verify positively with information I find. Maybe you have a link to OpenJDK wiki that says it all? box For HotSpot: The object header consists of a mark word and a klass pointer. The mark word has word size ( 4 byte on 32 bit architectures, 8 byte on 64 bit architectures) and the klass pointer has word size on 32 bit architectures. On 64

How can CopyOnWriteArrayList be thread-safe?

ⅰ亾dé卋堺 提交于 2019-11-26 11:54:08
问题 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 */

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

廉价感情. 提交于 2019-11-26 10:19:06
问题 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 does “volatile” mean in Java?

五迷三道 提交于 2019-11-26 09:47:37
问题 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. 回答1: 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