File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed
java-concurrency-issues-and-synchronization/src Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ public class MemoryConsistencyErrorExample {
2+ private static boolean sayHello = false ;
3+
4+ public static void main (String [] args ) throws InterruptedException {
5+
6+ Thread thread = new Thread (() -> {
7+ while (!sayHello ) {
8+
9+ }
10+
11+ System .out .println ("Hello World!" );
12+
13+ while (sayHello ) {
14+
15+ }
16+
17+ System .out .println ("Good Bye!" );
18+ });
19+
20+ thread .start ();
21+
22+ Thread .sleep (1000 );
23+ System .out .println ("Say Hello.." );
24+ sayHello = true ;
25+
26+ Thread .sleep (1000 );
27+ System .out .println ("Say Bye.." );
28+ sayHello = false ;
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ public class VolatileKeywordExample {
2+ private static volatile boolean sayHello = false ;
3+
4+ public static void main (String [] args ) throws InterruptedException {
5+
6+ Thread thread = new Thread (() -> {
7+ while (!sayHello ) {
8+
9+ }
10+
11+ System .out .println ("Hello World!" );
12+
13+ while (sayHello ) {
14+
15+ }
16+
17+ System .out .println ("Good Bye!" );
18+ });
19+
20+ thread .start ();
21+
22+ Thread .sleep (1000 );
23+ System .out .println ("Say Hello.." );
24+ sayHello = true ;
25+
26+ Thread .sleep (1000 );
27+ System .out .println ("Say Bye.." );
28+ sayHello = false ;
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments