java-memory-model

Behavior of memory barrier in Java

你。 提交于 2019-11-28 16:48:37
问题 After reading more blogs/articles etc, I am now really confused about the behavior of load/store before/after memory barrier. Following are 2 quotes from Doug Lea in one of his clarification article about JMM, which are both very straighforward: Anything that was visible to thread A when it writes to volatile field f becomes visible to thread B when it reads f. Note that it is important for both threads to access the same volatile variable in order to properly set up the happens-before

What JVM synchronization practices can I ignore assuming I know I will run on x64 cpus?

情到浓时终转凉″ 提交于 2019-11-28 11:11:04
I know that the JVM memory model is made for lowest common denominator of CPUs, so it has to assume the weakest possible model of a cpu on which the JVM can run (eg ARM). Now, considering that x64 has a fairly strong memory model, what synchronization practices can I ignore assuming I know my program will only run on 64bit x86 CPUs? Also does this apply when my program is being run through virtualization? Example: It is known that JVM's memory model requires synchronizing read/writes access to longs and doubles but one can assume that read/writes of other 32 bit primitives like int, float etc

why allocation phase can be increased if we override finalize method?

狂风中的少年 提交于 2019-11-28 10:22:22
问题 I have heard that in Joshua Bloch book written that allocation and memory collection might be increased to 430 times if we override finalize method. It is clear for me that memory collection can work slower because additional iteration requred for gc to free memory. But why allocation phase can be increased? 回答1: I have searched for the original statement: On my machine, the time to create and destroy a simple object is about 5.6 ns. Adding a finalizer increases the time to 2,400 ns. In other

Understanding happens-before and synchronization [duplicate]

夙愿已清 提交于 2019-11-28 10:08:54
This question already has an answer here: How to understand happens-before consistent 4 answers I'm trying to understand Java happens-before order concept and there are a few things that seem very confusing. As far as I can tell, happens before is just an order on the set of actions and does not provide any guarantees about real-time execution order. Actually (emphasize mine): It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results

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

依然范特西╮ 提交于 2019-11-28 08:40:39
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 before the writer thread performs any further memory operation (i.e., for the purposes at hand it is

Are hoisting and reordering the same thing?

空扰寡人 提交于 2019-11-28 07:42:36
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++; They are slightly different. Hoisting means that you have pulled some operation out of a loop because the loop itself does not affect the result of the operation. In your case,

Please explain initialization safety as spelled out in Java memory model

我只是一个虾纸丫 提交于 2019-11-27 20:34:02
问题 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 ? 回答1: 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

Immutability and reordering

寵の児 提交于 2019-11-27 17:20:27
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 thread observing the resource variable will either see it null or fully constructed (thanks to the strong

Memory effects of synchronization in Java

本小妞迷上赌 提交于 2019-11-27 17:08:12
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, we acquire the monitor, which has the effect of invalidating the local processor cache so that

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

断了今生、忘了曾经 提交于 2019-11-27 16:52:59
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 Model provides a listing of happens-before rules indicating which instruction ordering are preserved by