Borrowing one Example from JCIP
This commit is contained in:
parent
f79cdee39a
commit
35bbf9c884
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.concurrent.volatilekeyword;
|
||||
|
||||
public class TaskRunner {
|
||||
|
||||
private static int number;
|
||||
private volatile static boolean ready;
|
||||
|
||||
private static class Reader extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!ready) {
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
System.out.println(number);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Reader().start();
|
||||
number = 42;
|
||||
ready = true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue