8000 [WIP] add a simple and incomplete volatile example · fishercoder1534/RandomJava@4c45fad · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c45fad

Browse files
[WIP] add a simple and incomplete volatile example
1 parent 6961dab commit 4c45fad

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package java_volatile;
2+
3+
public class TaskRunner {
4+
private static int number;
5+
private static boolean ready;
6+
7+
private static class Reader extends Thread {
8+
@Override
9+
public void run() {
10+
System.out.println("ready is: " + ready);
11+
while (!ready) {
12+
System.out.println("It's yielding now..");
13+
Thread.yield();
14+
}
15+
System.out.println("number is: " + number);
16+
}
17+
}
18+
19+
public static void main(String[] args) {
20+
System.out.println("Program started.");
21+
new Reader().start();
22+
number = 42;
23+
ready = true;
24+
System.out.println("Program finished.");
25+
}
26+
}

0 commit comments

Comments
 (0)
0