Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 771ec33

Browse files
author
Rajeev Kumar Singh
committed
Volatile Keyword Example
1 parent 18ac416 commit 771ec33

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /