happens-before

Happens-before rules in Java Memory Model

旧时模样 提交于 2019-12-01 17:53:00
I am currently studying for a concurrent programming exam and don't understand why the output of this program is 43. Why is x = y + 1 executed before t.start() ? I also should explain which happens-before rules I used. If I understand the program order rule (each action in a thread happens-before every action in that thread that comes later in the program order) t.start() has to be executed before x = y + 1 so that thread t copies variable x which will be 1. public class HappensBefore { static int x = 0; static int y = 42; public static void main(String[] args) { x = 1; Thread t = new Thread()

Happens-before rules in Java Memory Model

前提是你 提交于 2019-12-01 17:32:55
问题 I am currently studying for a concurrent programming exam and don't understand why the output of this program is 43. Why is x = y + 1 executed before t.start() ? I also should explain which happens-before rules I used. If I understand the program order rule (each action in a thread happens-before every action in that thread that comes later in the program order) t.start() has to be executed before x = y + 1 so that thread t copies variable x which will be 1. public class HappensBefore {

How deep volatile publication guarantees?

一曲冷凌霜 提交于 2019-11-30 02:25:49
As is known guarant that if we have some object reference and this reference has final field - we will see all reachable fields from final field(at least when constructor was finished) example 1: class Foo{ private final Map map; Foo(){ map = new HashMap(); map.put(1,"object"); } public void bar(){ System.out.println(map.get(1)); } } As I undertand at this case we have guarantee that bar() method always output object because: 1. I listed full code of class Foo and map is final; 2. If some thread will see reference of Foo and this reference != null, then we have guarantees that reachable from

Memory Consistency - happens-before relationship in Java [duplicate]

半城伤御伤魂 提交于 2019-11-27 10:41:33
This question already has an answer here: How to understand happens-before consistent 4 answers While reading Java docs on Memory Consistency errors. I find points related to two actions that creates happen - before relationship: When a statement invokes Thread.start() , every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the creation of the new thread are visible to the new thread. When a thread terminates and causes a Thread.join() in another

Memory Consistency - happens-before relationship in Java [duplicate]

左心房为你撑大大i 提交于 2019-11-26 15:17:06
问题 This question already has answers here : How to understand happens-before consistent (4 answers) Closed last year . While reading Java docs on Memory Consistency errors. I find points related to two actions that creates happen - before relationship: When a statement invokes Thread.start() , every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the