Questions tagged [java-memory-model]
The Java Memory Model (JMM) describes which executions of a program are legal by determining what value(s) can be observed when reading a shared variable according to certain rules.
java-memory-model
390
questions
0
votes
1
answer
77
views
Understanding the volatile Modifier in the Context of x86 Architecture and the Java Memory Model (JMM)
I have a question regarding the Java Memory Model (JMM), particularly in the context of x86 architecture, which I find quite intriguing. One of the most confusing and often debated topics is the ...
0
votes
2
answers
67
views
Java Memory UTF-16 Vs UTF-8
By Default Java stores strings in UTF-16, in my application it is using huge memory. One of the suggestion we get is to convert UTF-16 to UTF-8 so some memory can be saved. is this True ?
If yes Can I ...
0
votes
2
answers
72
views
Is the Happens-Before mechanism only hb(w, r)?
I'm trying to understand the Happens Before mechanism and i can't find any source in the internet talking about happens-before relationship between a READ and subsequent WRITE. I can only read about ...
2
votes
1
answer
57
views
Does Kotlin val give the same visibility guarantees as java final?
I have a class like this:
class MyClass {
private val myLock = ReentrantLock()
...
}
In java I would make this field final to provide memory guarantees:
https://stackoverflow.com/a/27254652/...
2
votes
1
answer
133
views
Is it possible to guarantee safe publication of a non-final field in Java?
Let's imagine we have some class with one volatile non-final field that we want to initialise with a default value passed through a constructor:
public class MyClass {
private volatile String s;
...
1
vote
0
answers
70
views
Does a member variable need to be volatile if no concurrent writes occur?
If a member variable is updated by one thread and later read (not updated) by other treads from a thread pool, does that member variable needs to be declared as volatile?
Made-up code to illustrate ...
2
votes
1
answer
71
views
ConcurrentHashMap - Can we get rid of i >= n from transfer()?
Related to :
In ConcurrentHashMap's transfer method, I don't understand the meaning of these two conditions "i >= n" and "i + n >= nextn"
I am looking into the ...
4
votes
2
answers
109
views
Is it safe to assume that everything happened in a constructor is visible to threads running methods after object initialization?
Lets have the following class:
public class MyClass {
private final List<String> myList = new ArrayList<>(); //Not a thread-safe thing
// Called on thread 1
MyClass() {
myList....
1
vote
3
answers
135
views
How to use volatile to ensure sequential consistency
Consider two threads:
A==B==0 (initially)
Thread 1
Thread 2
B=42;
if (A==1)
A=1;
...print(B)
To my knowledge if (at least) A is volatile we will only be able to read B==42 at the print. Though if ...
0
votes
1
answer
58
views
Writing to a volatile field and reading another volatile field: Is the happens-before-rule valid?
Question: Is this statement true: „A write to a volatile field happens-before every subsequent read of that or another volatile field.“
On How to understand happens-before consistent I found the ...
1
vote
0
answers
108
views
How does the JMM qualifies the usage of local variables inside lambdas? [duplicate]
I don't understand how the JMM qualifies the usage of local variables inside lambdas (and also in local and anonymous classes).
It looks like they aren't "shared variables" in terms of the ...
0
votes
1
answer
210
views
Project Reactor and Safe Publication
I need your help. There is a reactive chain in a Spring WebFlux application that uses R2dbcRepository:
entityRepository //0
.findById(entityId) //1 Mono<Entity>
.doOnNext(e-> e....
-1
votes
2
answers
48
views
How can this BitSet example print (false,true) or (true, false)?
I am trying to understand the JMM by following the blog post by Aleksey Shipilëv
The above example is breaking my mind.
Explanation: There are 3 threads the first 2 threads (the 1st and 2nd column) ...
2
votes
1
answer
138
views
Volatile happens-before clarification/misunderstanding?
"A write to a volatile field (§8.3.1.4) happens-before every subsequent read of that field."
So I know that volatile field can be used as synchronization in order to guarantee that that all ...
2
votes
1
answer
69
views
JMM: show that happens-before relation is stronger that RA causality
New memory ordering where added in JDK9, so I'm digging into Release/Acquire mode. It introduce causality constraint:
If access A precedes interthread Release mode (or stronger) write W in source ...