-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolatileDemo.java
More file actions
38 lines (27 loc) · 740 Bytes
/
VolatileDemo.java
File metadata and controls
38 lines (27 loc) · 740 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
32
33
34
35
36
37
38
package com.codecafe.concurrency._volatile;
class Task implements Runnable {
private volatile boolean isRunning = true;
@Override
public void run() {
while (isRunning) {
System.out.println(Thread.currentThread().getName() + " running");
}
}
public void shutdown() {
isRunning = false;
System.out.println("terminated by : " + Thread.currentThread().getName());
}
}
public class VolatileDemo {
public static void main(String[] args) throws InterruptedException {
Task task = new Task();
new Thread(task, "T1").start();
new Thread(task, "T2").start();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
task.shutdown();
}
}