memory-visibility

jvm reordering/visibility effect test

别说谁变了你拦得住时间么 提交于 2020-02-02 03:43:03
问题 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 synchonization/volatiles/finals and other threads get access to it right after constructor call. Here is the code I try: public class ReorderingTest { static SomeObject<JPanel>[] sharedArray = new SomeObject[100]; public static void main(String[] args) { for (int i = 0; i < 100; i++) { String name =

Why does this simple threaded program get stuck?

本秂侑毒 提交于 2020-01-13 13:31:59
问题 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() { public void run() { try { Thread.sleep(1000); // dummy work load } catch (Exception e) { done = true; } done = true; } }.start(); while (!done); System.out.println("bye"); } } On one machine, it prints "bye" and exits right away, while on another machine, it doesn't print anything and sits there forever. Why? 回答1: This is because

Why does this simple threaded program get stuck?

情到浓时终转凉″ 提交于 2020-01-13 13:31:08
问题 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() { public void run() { try { Thread.sleep(1000); // dummy work load } catch (Exception e) { done = true; } done = true; } }.start(); while (!done); System.out.println("bye"); } } On one machine, it prints "bye" and exits right away, while on another machine, it doesn't print anything and sits there forever. Why? 回答1: This is because

Does atomic variables guarantee memory visibility?

可紊 提交于 2019-12-22 04:34:23
问题 Small question about memory visibility. CodeSample1: class CustomLock { private boolean locked = false; public boolean lock() { if(!locked) { locked = true; return true; } return false; } } This code is prone to bugs in a multi-threaded environment, first because of the "if-then-act" which is not atomic, and second because of potential memory visibility issues where for example threadA sets the field to true, but threadB that later wishes to read the field's value might not see that, and

Atomic operation propagation/visibility (atomic load vs atomic RMW load)

女生的网名这么多〃 提交于 2019-12-21 05:44:30
问题 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 understanding of atomics and lock-free programming, but I do not have enough expertise to optimise my code. I did a lot of researching, but it was hard to find answers to my specific problem: What is the propagation delay/visiblity for different atomic operations under different memory orders? Current

Uninitialized object leaked to another thread despite no code explicitly leaking it?

情到浓时终转凉″ 提交于 2019-12-17 09:55:57
问题 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 void main(String[] args) { new Thread() { void f(B q) { int x = q.x; if (x != 1) { System.out.println(x); System.exit(1); } } @Override public void run() { while (b == null); while (true) f(b); } }.start(); for (int x = 0;;x++) b = new B(Math.max(x%2,1)); } } Main thread The main thread creates an instance of B with x set to 1, then writes that

Does a volatile reference really guarantee that the inner state of the object is visible to other threads?

微笑、不失礼 提交于 2019-12-12 03:48:58
问题 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 at the same time. A properly constructed object can be safely published by: Initializing an object reference from a static initializer; Storing a reference to it into a volatile field or AtomicReference; Storing a reference to it into a final field of a properly constructed object; or Storing a

Why does this simple threaded program get stuck?

流过昼夜 提交于 2019-12-05 20:28:27
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() { public void run() { try { Thread.sleep(1000); // dummy work load } catch (Exception e) { done = true; } done = true; } }.start(); while (!done); System.out.println("bye"); } } On one machine, it prints "bye" and exits right away, while on another machine, it doesn't print anything and sits there forever. Why? This is because your boolean is not volatile , therefore Thread s are allowed to cache copies of it and never update them. I

jvm reordering/visibility effect test

不羁岁月 提交于 2019-12-05 10:17:10
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 synchonization/volatiles/finals and other threads get access to it right after constructor call. Here is the code I try: public class ReorderingTest { static SomeObject<JPanel>[] sharedArray = new SomeObject[100]; public static void main(String[] args) { for (int i = 0; i < 100; i++) { String name = "watcher" + i; new Thread(new Watcher(name)).start(); System.out.printf("watcher %s started!%n", name); } }

Atomic operation propagation/visibility (atomic load vs atomic RMW load)

拟墨画扇 提交于 2019-12-03 17:38:03
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 understanding of atomics and lock-free programming, but I do not have enough expertise to optimise my code. I did a lot of researching, but it was hard to find answers to my specific problem: What is the propagation delay/visiblity for different atomic operations under different memory orders? Current assumptions   I read that changes to memory are propagated from other threads, in such a way that they