39 questions
0
votes
1
answer
114
views
Is this piece of code correctly synchronized? Java synchronization and memory visibility question
Here is a Java class that writes messages in a parallel thread to a stream. When the stop() method is called, the mCloseWriter flag is set and the thread that writes the messages to the stream is ...
3
votes
0
answers
50
views
Does `AsynchronousSocketChannel.write` and `read` methods apply a memory fence before calling the `CompletionHandler` methods?
When using AsynchronousSocketChannel.write to transmit a message,
class Transmitter(channel: AsynchronousSocketChannel) {
def transmit(bytes: Array[Byte])(onComplete: Consumer[String]): ...
0
votes
1
answer
145
views
Java memory visibility outside the synchronized lock
Does the synchronized lock guarantee the following code always print 'END'?
public class Visibility {
private static int i = 0;
public static void main(String[] args) throws ...
3
votes
1
answer
239
views
When does freeze action happen in java constructor?
I am confused about final field semantics in java. I've read 17.5 paragraph jls and found this example:
class FinalFieldExample {
final int x;
int y;
static FinalFieldExample f;
...
1
vote
2
answers
418
views
Does synchronisation with ConcurrentHashMap's .compute() guarantee visibility?
Within ConcurrentHashMap.compute() I increment and decrement some long value located in shared memory. Read, increment/decrement only gets performed within compute method on the same key.
So the ...
5
votes
0
answers
265
views
Visibility guarantee with CompletableFuture's thenAccept
I'm trying to understand if there are any visibility-guarantees provided by CompletableFuture.
Suppose I've a class called SampleClass which is something like the following:
public class SampleClass ...
1
vote
1
answer
1k
views
Which ARM memory barrier guarantees that one of two threads will observe what the other thread wrote?
Threads A and B are executing concurrently. Which ARMv8-A memory barrier types (like DMB, DSB) are sufficient to fulfill the postcondition, and why?
Initially x1 = 0, x2 = 0
Thread A | ...
5
votes
3
answers
2k
views
Atomic operation propagation/visibility (atomic load vs atomic RMW load)
Context
I am writing a thread-safe protothread/coroutine library in C++, and I am using atomics to make task switching lock-free. I want it to be as performant as possible. I have a general ...
4
votes
2
answers
715
views
C++11 atomics. visibility and thread.join() / correct way to stop a thread
For which (if any?) STORE_ORDER & LOAD_ORDER does C++11 guarantee that this code runs in finite time?
std::atomic<bool> a{false};
std::thread t{[&]{
while(!a.load(LOAD_ORDER));
}};
a....
4
votes
0
answers
391
views
How to demonstrate memory visibility problems in Go?
I'm giving a presentation about the Go Memory Model. The memory model says that without a happens-before relationship between a write in one goroutine, and a read in another goroutine, there is no ...
0
votes
0
answers
64
views
Fill a singleton once and access data from all threads in java
I am on my cell phone now, so I have no code here. But it's simple.
I have one static class working as a singleton in a web app.
That class is called on the start of the application (when I use to ...
14
votes
3
answers
2k
views
Java ConcurrentHashMap.computeIfPresent value modification visibility
Let's say I have a concurrent map with collections as value:
Map<Integer, List<Integer> map = new ConcurrentHashMap<>();
map.putIfAbsent(8, new ArrayList<>());
and I update the ...
4
votes
1
answer
180
views
If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?
I've looked at this answer, and it states how:
Under the new memory model, when thread A writes to a volatile
variable V, and thread B reads from V, any variable values that were
visible to A at ...
0
votes
0
answers
253
views
Spring and Java visibility
Lately our system has been experiencing all kinds of Spring related 'voodoo'. It usually goes something like this:
Various singleton beans have (ctor) autowired properties, based on which they ...
0
votes
0
answers
41
views
Are operations before Thread.start() thread safe in relation to the new thread? [duplicate]
Are there any guarantees to the variable visibility and/or ordering of instructions before Thread.start() in relation to the starting of the new thread?
Specifically, in the minimalistic example ...
1
vote
1
answer
273
views
Is it thread safe that java anonymous inner-class implements Runnable and shared outer-class object?
public class Main {
public static void main(String[] args) {
Status status = new Status();
new Thread(new Runnable() {
@Override
public void run() {
...
13
votes
2
answers
2k
views
Does atomic variables guarantee memory visibility?
Small question about memory visibility.
CodeSample1:
class CustomLock {
private boolean locked = false;
public boolean lock() {
if(!locked) {
locked = true;
...
3
votes
1
answer
693
views
Does a volatile reference really guarantee that the inner state of the object is visible to other threads?
While reading "Java Concurrency in Practice", I came across the following -
To publish an object safely, both the reference to the object and the object's state must be made visible to other threads ...
2
votes
2
answers
85
views
The main thread randomly doesn't reach end (trying to sum natural numbers in concurrent threads) [duplicate]
I have the following code to find the sum of natural numbers from 1 to 5000. It's a simple exercise to practice concurrency.
public static void main(String[] args) throws InterruptedException {
...
6
votes
3
answers
409
views
Visibility Guarantee
I have read a few explanations of section 16.3 "Initialization Safety" from JCIP and am still not clear. The section states that
"Further, any variables that can be reached through a final field of a ...
2
votes
2
answers
432
views
Visibility issue in java concurrent programming
I came across following example in book 'Java Concurrency in Practice'.
public class NoVisibility {
private static boolean ready;
private static int number;
private static class ...
4
votes
3
answers
697
views
Is volatile needed?
if I have a byte queue, where it is expected to have one thread producer, another consumer:
class ByteQueue{
byte[] buf;
/*volatile?*/ int readIdx;
/*volatile?*/ int writeIdx;
...
2
votes
2
answers
250
views
Java Concurrent Collections and visiblity
I'm getting a little unsure about what to expect from Concurrent Collections (e.g. ConcurrentMap) regarding visibility of the data in the collection.
A: Thread1 puts a complex object and Thread2 gets ...
2
votes
3
answers
209
views
Perl ithreads :shared variables - multiprocessor kernel threads - visibility
perlthrtut excerpt:
Note that a shared variable guarantees that if two or more threads try
to modify it at the same time, the internal state of the variable will
not become corrupted. However, ...
0
votes
2
answers
156
views
Does BackgroundWorker guarantee that memory changes made on the background thread become visible to the main thread?
If I use a BackgroundWorker to modify data structures in my application, is there a guarantee that changes made on the background thread will be visible to the main (UI) thread when the ...
4
votes
3
answers
748
views
Volatile array - memory visibility of the elements
Consider the code snippet
class A {
private Map<String, Object> taskMap = new HashMap<>();
private volatile Object[] tasksArray ;
// assume this happens on thread1
public ...
3
votes
1
answer
402
views
Using volatile to ensure visibility of shared (but not concurrent) data in Java
I'm trying to implement a fast version of LZ77 and I have a question to ask you about concurrent programming.
For now I have a final byte[] buffer and a final int[] resultHolder, both of the same ...
3
votes
3
answers
1k
views
Visibility in concurrent C++ programs
I know in Java visibility of a member is not guaranteed when accessing it from another thread.
The meaning is the accessing thread will maybe see a stole value of the member (becuase the cache has ...
1
vote
1
answer
199
views
Are memory visibility problems per thread also there if there is just one CPU core?
Title might seem weird, so let me explain.
Often people teaching about race conditions say thread1 can see x == 0 while thread 2 already did x=1;
My questions is about threads scheduled on same core(...
4
votes
1
answer
194
views
Assigning the value of an array variable to itself?
I have this simple piece of code.
class A {
static volatile String[] a = new String[9];
public static void main(String[] args) {
new Thread() {
public void run() {
...
4
votes
3
answers
2k
views
Uninitialized object leaked to another thread despite no code explicitly leaking it?
Let's see this simple Java program:
import java.util.*;
class A {
static B b;
static class B {
int x;
B(int x) {
this.x = x;
}
}
public static ...
210
votes
5
answers
51k
views
Why does this Java program terminate despite that apparently it shouldn't (and didn't)?
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 ...
3
votes
2
answers
629
views
Why does this simple threaded program get stuck?
Take a look at this simple Java program:
import java.lang.*;
class A {
static boolean done;
public static void main(String args[]) {
done = false;
new Thread() {
...
17
votes
2
answers
2k
views
Volatile variables and other variables
The following is from classical Concurency in Practice:
When thread A writes to a volatile variable and subsequently thread B
reads the same variable, the values of all variables that were
...
12
votes
5
answers
3k
views
Is unsynchronized read of integer threadsafe in java?
I see this code quite frequently in some OSS unit tests, but is it thread safe ? Is the while loop guaranteed to see the correct value of invoc ?
If no; nerd points to whoever also knows which CPU ...
14
votes
4
answers
413
views
in what architectures/OS other thread can see default nonfinal field values after constructor call?
I'm trying to reproduce a memory visibility issue in case of insufficient object initialization for non-final fields (JLS 17.5 Final Field Semantics, FinalFieldExample class example). Where it stated "...
6
votes
4
answers
806
views
jvm reordering/visibility effect test
While writing some java article I'm trying to reproduce re-ordering in case of unsynchronized object costruction in multithreaded environment.
The case when a heavy object is constructed w/o ...
1
vote
2
answers
444
views
Ensuring memory visibility with builder/factory pattern
The following class:
class Pizza {
Ingredients ingredients;
Price price;
public setIngredients(Ingredients ing) {
if (ingredients != null) {
throw new ...
102
votes
7
answers
117k
views
Are static variables shared between threads?
My teacher in an upper level Java class on threading said something that I wasn't sure of.
He stated that the following code would not necessarily update the ready variable. According to him, the ...