-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathVolatile.java
More file actions
31 lines (27 loc) · 879 Bytes
/
Volatile.java
File metadata and controls
31 lines (27 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package VisibilityProblem;
import VisibilityProblem.SharedFlag;
public class Volatile{
public static void main(String[] args) {
SharedFlag flag = new SharedFlag();
//Thread 1
new Thread(() -> {
try {
System.out.println("Thread 1 started");
Thread.sleep(1000);
flag.setFlag(true);
System.out.println("Flag set to True by thread 1 ");
System.out.println("Thread 1 Completed");
}catch (InterruptedException e){
e.printStackTrace();
}
}).start();
//Thread 2
new Thread(() -> {
System.out.println("Thread 2 started");
while(!flag.isFlag()) {
//some processing
}
System.out.println("Thread 2 Completed");
}).start();
}
}