memory-visibility

If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?

安稳与你 提交于 2019-11-30 10:13:14
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 the time that V was written are guaranteed now to be visible to B. Therefore, given the example: public class Main { static int value = -1; static volatile boolean read; public static void main(String[] args) { Thread a = new Thread(() -> { value = 1; read = true; }); Thread b = new Thread(() -> { while (!read); System.out.println("Value: " + value); }); a.start(); b.start(); } } Is the change to

If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?

拈花ヽ惹草 提交于 2019-11-29 15:55:05
问题 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 the time that V was written are guaranteed now to be visible to B. Therefore, given the example: public class Main { static int value = -1; static volatile boolean read; public static void main(String[] args) { Thread a = new Thread(() -> { value = 1; read = true; }); Thread b = new Thread(() -> {

Why does this Java program terminate despite that apparently it shouldn't (and didn't)?

江枫思渺然 提交于 2019-11-27 09:57:45
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 down over 40K lines in the faulty module to this: import java.util.*; class A { static Point currentPos = new Point(1,2); static class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } } public static void main(String[] args) { new Thread() { void f(Point p) { synchronized(this) {} if (p.x+1 != p.y) { System.out.println(p.x+" "+p.y); System.exit(1); } } @Override public void run(

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

此生再无相见时 提交于 2019-11-27 09:20:24
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 instance to the static field A.b . It repeats this action forever. Polling thread The spawned thread polls

Volatile variables and other variables

有些话、适合烂在心里 提交于 2019-11-26 22:51:27
问题 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 visible to A prior to writing to the volatile variable, become visible to B after reading the volatile variable. I am not sure I can really understand this statement. For example, what is the meaning of all variables in this context? Does this mean that using volatile also has side-effects to the usage of

Why does this Java program terminate despite that apparently it shouldn't (and didn't)?

前提是你 提交于 2019-11-26 14:58:15
问题 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 down over 40K lines in the faulty module to this: import java.util.*; class A { static Point currentPos = new Point(1,2); static class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } } public static void main(String[] args) { new Thread() { void f(Point p) { synchronized(this)

Are static variables shared between threads?

梦想的初衷 提交于 2019-11-26 11:11:18
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 two threads don't necessarily share the static variable, specifically in the case when each thread (main thread versus ReaderThread ) is running on its own processor and therefore doesn't share the same registers/cache/etc and one CPU won't update the other. Essentially, he said it is possible that ready is updated in the main thread, but NOT in the ReaderThread , so that ReaderThread will loop

Are static variables shared between threads?

女生的网名这么多〃 提交于 2019-11-26 02:06:53
问题 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 two threads don\'t necessarily share the static variable, specifically in the case when each thread (main thread versus ReaderThread ) is running on its own processor and therefore doesn\'t share the same registers/cache/etc and one CPU won\'t update the other. Essentially, he said it is possible