-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolatile2.java
More file actions
26 lines (21 loc) · 758 Bytes
/
Volatile2.java
File metadata and controls
26 lines (21 loc) · 758 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
public class Volatile2 {
public static volatile boolean flag = false;
public static void main(String args[]){
Runnable MyRunnable = () -> {
System.err.println("Inside Thread!");
while(!flag){
System.out.println("Infinite loop!");
}
System.out.println("Flag is found to be false, so terminating. Inside thread");
};
Thread t1 = new Thread(MyRunnable);
t1.start();
try {
Thread.sleep(1000); // Ensure thread starts before flag is updated
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = true;
System.out.println("Flag set to false by main!");
}
}