java-memory-model

Synchronized data read/write to/from main memory

本小妞迷上赌 提交于 2019-12-07 09:16:16
问题 When a synchronized method is completed, will it push only the data modified by it to main memory, or all the member variables, similarly when a synchronized method executes, will it read only the data it needs from main memory or will it clear all the member variables in the cache and read their values from main memory ? For example public class SharedData { int a; int b; int c; int d; public SharedData() { a = b = c = d = 10; } public synchronized void compute() { a = b * 20; b = a + 10; }

Does Java reordering affect System.currentTimeMillis()?

ぃ、小莉子 提交于 2019-12-06 17:04:22
问题 According to Java Memory Model, instructions can be reordered as long as the execution is well-formed. So I wonder, is it possible that the following codes produces the following output? [codes][in a same thread] long a = System.currentTimeMillis(); long b = System.currentTimeMillis(); long c = System.currentTimeMillis(); [output] a == 10, b == 20, c == 15 If not possible, then what does JVM / implementations do to prevent this from happening? 回答1: Please see this question Instruction

Is it possible to efficiently implement a seqlock in Java?

非 Y 不嫁゛ 提交于 2019-12-06 04:37:29
问题 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];

Does this example contain a data race?

馋奶兔 提交于 2019-12-06 03:54:41
Here is the originan question, but mine have some differences with it. C++ memory model - does this example contain a data race? My question: //CODE-1: initially, x == 0 and y == 0 if (x) y++; // pthread 1 if (y) x++; // pthread 2 Note: the code above is written in C, not C++ (without a memory model). So does it contain a data race? From my point of view: if we view the code in Sequential Consistency memory model, there is no data race because x and y will never be both non-zero at the same time. However, we can never assume a Sequential Consistency memory model, so the compilier reordering

Sequential consistency volatile explanation

两盒软妹~` 提交于 2019-12-06 03:45:25
I am watching video from java jpoint conference. I have question about following slide from Alexey Shipilev report: Excuse me for non-english on slide. Actually author says that it is impossible that variable set will be r1 = 1 (Y) r2 = 0 (x) r3 = 1 (x) r4 = 0 (Y) According the video he implies that it is obviously. Can someone clarify why this value set impossible according JMM? P.S. If I understand Alexey notation correct it respects the following code: public class SequentialConsistency { static volatile int x; static volatile int y; public static void main(String[] args) { new Thread(new

String pool is created in PermGen area or Object area of Heap

与世无争的帅哥 提交于 2019-12-06 02:53:50
问题 HERE, author is saying that 3) String pool is created in PermGen area of Heap, garbage collection can occur in perm space but depends upon JVM to JVM. By the way from JDK 1.7 update, String pool is moved to heap area where objects are created. Is there any specific reason why is it done ? I am not able to find any online. And what are the implications ? 回答1: The move to Metaspace was necessary since the PermGen was really hard to tune. Also, it was difficult to size the PermGen since the size

Is setting a HashMap thread safe?

帅比萌擦擦* 提交于 2019-12-05 20:24:01
问题 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

Java happend before thread start

房东的猫 提交于 2019-12-05 18:43:34
I read somewhere that starting a thread has some special effect on the happend before relationship. Now I'm not sure if my code gurantees the happend before relationship, so please enlighten me. I have a Dispatcher thread and a Worker class implementing the Runnable interface. The Dispatcher thread creates a new instance of the Worker and fills a LinkedList in the Worker instance through the add method with elements. Then the Dispatcher hands the Worker instance to a ExecutorService via the execute method. Then the run method in the Worker class starts accessing and removing stuff from the

Synchronized data read/write to/from main memory

邮差的信 提交于 2019-12-05 16:10:54
When a synchronized method is completed, will it push only the data modified by it to main memory, or all the member variables, similarly when a synchronized method executes, will it read only the data it needs from main memory or will it clear all the member variables in the cache and read their values from main memory ? For example public class SharedData { int a; int b; int c; int d; public SharedData() { a = b = c = d = 10; } public synchronized void compute() { a = b * 20; b = a + 10; } public synchronized int getResult() { return b*c; } } In the above code assume compute is executed by

Java Memory Model: Is it safe to create a cyclical reference graph of final instance fields, all assigned within the same thread?

寵の児 提交于 2019-12-05 13:14:25
问题 Can somebody who understand the Java Memory Model better than me confirm my understanding that the following code is correctly synchronized? class Foo { private final Bar bar; Foo() { this.bar = new Bar(this); } } class Bar { private final Foo foo; Bar(Foo foo) { this.foo = foo; } } I understand that this code is correct but I haven't worked through the whole happens-before math. I did find two informal quotations that suggest this is lawful, though I'm a bit wary of completely relying on