402 questions
0
votes
3
answers
211
views
What is the relationship between JMM (Java Memory Model) and JVM (Java Virtual Machine)? [closed]
I often see both terms — JMM (Java Memory Model) and JVM (Java Virtual Machine) — when learning about multithreading and memory management in Java. However, I'm confused about how they relate to each ...
0
votes
1
answer
74
views
Are operations to ImmutableCollection saved to non-final field thread-safe
If in my class I have a field of type ImmutableMap like:
private ImmutableMap<String, State> states = ImmutableMap.of();
And there is 1 reader thread and 1 writer thread
Reader reads by calling ...
3
votes
1
answer
195
views
Does CompletableFuture ensure field update visibility after join() in Java?
I’m working with CompletableFuture in Java and want to understand how field updates made inside a CompletableFuture task are visible to the main thread after calling join(). Specifically, if I pass an ...
4
votes
1
answer
213
views
Does Java permit unbounded starvation?
Introduction
Consider this Java program:
public class Loop {
static volatile boolean flag;
public static void main(String[] args) {
new Thread(() -> flag = true).start();
...
1
vote
2
answers
101
views
Why does the Java Memory Model allow reads to observe future writes?
There seems to be a similar question (Java Specification: reads see writes that occur later in the execution order), but it focuses on a different example.
The Java Memory Model states in §17.4.8:
...
3
votes
1
answer
145
views
Can r1 == 0 and r2 == 0 occur without volatile in this Java program?
Do I understand correctly that if I run run1() and run2() in parallel, the result r1 == 0 and r2 == 0 is impossible under any circumstances, even though a and b are not volatile?
public class Example {...
0
votes
0
answers
64
views
Is a Noop JVM valid under the Java Memory Model?
I'm trying to understand the Memory Model chapter from the Java Language Specification, but I'm already confused by the first two paragraphs:
A memory model describes, given a program and an ...
1
vote
1
answer
131
views
How does memory allocation work when calling lambdas in Java? Can lambdas be used in a GC-free style for latency-critical applications?
I am trying to understand how Java handles memory allocation for lambdas and whether it's possible to use them in a GC-free style for latency-critical applications. My goal is to determine if lambdas ...
4
votes
2
answers
193
views
Double checked locking pattern with final
I was reading this: https://en.m.wikipedia.org/wiki/Double-checked_locking
And in section Usage in Java, last example:
Semantics of final field in Java 5 can be employed to safely publish the helper ...
-1
votes
1
answer
85
views
How to prove the volatile visibility guarantee?
I am reading an article about the Java Volatile keyword, got some questions. click here
public class SharedObject {
public int counter = 0;
}
Imagine too, that only Thread 1 increments the ...
1
vote
1
answer
88
views
Data race between static initializer and constructor of different classes
Per the JMM, section 9.2.3
The rules for class initialization ensure that any thread that reads a static field will be synchronized
with the static initialization of that class.
I am trying to ...
1
vote
1
answer
84
views
The reordering of Java Memory Model
@JCStressTest
@Outcome(id = "1, 1", expect = Expect.ACCEPTABLE, desc = "ordered")
@Outcome(id = "0, 1", expect = Expect.ACCEPTABLE, desc = "ordered")
@...
0
votes
4
answers
453
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
113
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
83
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
76
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
177
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
75
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
81
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
168
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
168
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 only ...
0
votes
1
answer
62
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
117
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
329
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
56
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
169
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
85
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 ...
0
votes
2
answers
154
views
How to properly use synchronized block or locks to ensure variable visibility?
Sample code:
public class TestTestTest {
private void setFalseFlag() {
this.keepRunning = false;
System.out.println("keepRunning is false");
}
private boolean ...
1
vote
0
answers
34
views
JMM: Observable Behavior and Nonterminating Executions
Can someone explain to me the meaning of 17.4.9. Observable Behavior and Nonterminating Executions.
What I understand (kind of):
this section exists because of the programs which never terminate:
...
1
vote
2
answers
137
views
Could "correctly synchronized" be applied to a class instead of the whole program?
There is a term correctly synchronized in the JLS:
A program is correctly synchronized if and only if all sequentially consistent executions are free of data races.
If a program is correctly ...
2
votes
1
answer
192
views
Why no data races in sequentially consistent executions is enough to guarantee that there will be no data races in all executions?
According to the Java Memory Model (JMM):
A program is correctly synchronized if and only if all sequentially consistent executions are free of data races.
If a program is correctly synchronized, ...
2
votes
2
answers
159
views
Why is the sufficient set of synchronization edges unique and how to build it?
JLS states this:
A set of synchronization edges, S, is sufficient if it is the minimal set such that the transitive closure of S with the program order determines all of the happens-before edges in ...
2
votes
3
answers
999
views
Do I need to use volatile?
Consider the following code:
public class MyDataStructure {
int size;
final ReentrantLock lock = new ReentrantLock();
public void update() {
lock.lock();
try {
...
3
votes
1
answer
110
views
JMM: Why this outcome is illegal?
I recently stumbled upon this example in jcstress:
@JCStressTest
@State
@Outcome(id = "10", expect = ACCEPTABLE, desc = "Boring")
@Outcome(id = {&...
4
votes
2
answers
368
views
Java volatile memory ordering and its compilation on x86-64
Consider the following simple Java application:
public class Main {
public int a;
public volatile int b;
public void thread1(){
int b;
a = 1;
b = this.b;
}
...
1
vote
1
answer
155
views
How happens-before covers single variable write and read?
When dealing with happens before, I see it as about dealing with memory ordering and whether some memory ordering is valid with program order. For example, take the Dekker algorithm:
public class ...
2
votes
1
answer
54
views
Is it possible to reproduce non-compositional queues example in Java?
I'm reading The Art of Multiprocessor Programming, 2nd ed.
Sequential consistency is defined there this way:
Sequential consistency requires that method calls act as if they occurred in a sequential ...
1
vote
4
answers
249
views
Visibility of a read after synchronized block
Does JMM guarantee the visibility of a synchronized write to the variable that is read in the other thread after a synchronized block? Here's what I mean:
public class SynchronizedWriteRead {
...
8
votes
1
answer
254
views
How are the final multi-threading guarantees and the memory model related in Java?
The memory model is defined in 17.4. Memory Model.
The final field multi-threading guarantees are given in 17.5. final Field Semantics.
I don't understand why these are separate sections.
AFAIK both ...
17
votes
3
answers
786
views
Does Java allow a volatile read to be optimized away if the value isn't needed, also removing the happens-before synchronization?
The following code sample shows a common way to demonstrate concurrency issues caused by a missing happens-before relationship.
private static /*volatile*/ boolean running = true;
public static ...
5
votes
2
answers
404
views
What does "execution trace" mean in Java Memory Model
The part of the language specification dedicated to the Java Memory Model (JMM) (link) mentions "execution trace" a lot.
For example right from the start:
A memory model describes, given a ...
0
votes
1
answer
86
views
java array elements memory consistency
A very naive array based circular buffer implementation,
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class SomeRingBuffer {
...
2
votes
1
answer
393
views
what does Java Volatile Read really do?
I have a very confused question about java volatile read.
I will show two cases to explain my question.
case1:
class TestVolatile {
public boolean running = true;
public volatile boolean ...
2
votes
2
answers
556
views
Is JVM working memory the same as CPU cache
The JVM spec says that each thread has its own working memory and lists several operations including use, assign, load, read, store, write, lock, unlock. Is working memory the same as the CPU cache, ...
3
votes
1
answer
778
views
Need clarification on memory usage of a jar
I'm running a jar using this command. Note the memory parameters. (The jar size is 56MB)
java -jar -Xms64M -Xmx256M build/libs/account-1.0.0-SNAPSHOT.jar
In my Ubuntu System Monitor, the jar memory ...
0
votes
1
answer
162
views
How can a store to a variable be reordered after a load of that variable, given single-threaded serialization guarantee?
Lets have a simple code snippet
import java.invoke.VarHandle.fullFence;
...
int x = 42;
int y = 42;
...
x = 1;
fullFence();
x = 0;
y = x;
fullFence();
//another ...
0
votes
2
answers
199
views
Do I need getVolatile memory access semantics if the value is set with setVolatile?
Now, before some zealot quotes Knuth forgetting what he spent his life on - the question has a mostly educational purpose, as I struggle to understand memory barriers.
Lets assume:
public class Var&...
1
vote
1
answer
341
views
Why release fences added to Scala collections in 2.13 are enough in absence of matching acquire fences?
The process of creation of some of Scala immutable collections, most notably List, is mutable. In 2.13 the concurrency issues were addressed by adding a release fence basically to every builder. A ...
0
votes
2
answers
183
views
Does the java memory model prevent interleaving writes from different threads between volatile variables?
I was reading an interesting article about the memory barriers and their role in JVM concurrency, and the example implementation of Dekker's algorithm took my attention
volatile boolean ...
5
votes
4
answers
716
views
Can release+acquire break happens-before?
Many programming languages today have happens-before relation and release+acquire synchronization operations.
Some of these programming languages:
C/C++11: happens-before, release+acquire
Rust and ...