-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopRunnable.java
More file actions
58 lines (48 loc) · 1.71 KB
/
Copy pathStopRunnable.java
File metadata and controls
58 lines (48 loc) · 1.71 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package multithreading.basics;
/**
* Creates a {@link Runnable} that keeps running till the main method requests it to stop.
*/
public class StopRunnable {
public static class StoppableRunnable implements Runnable {
private boolean stopRequested = false;
public synchronized void requestStop() {
this.stopRequested = true;
}
public synchronized boolean isStopRequested() {
return this.stopRequested;
}
private void sleep(long millis) {
try{
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " started.");
// this thread will keep running till the main thread flips the stopRequested flag.
while (!isStopRequested()) {
sleep(1000);
System.out.println(Thread.currentThread().getName() + " running...");
}
System.out.println(Thread.currentThread().getName() + " stopped.");
}
}
public static void main(String[] args) {
StoppableRunnable stoppableRunnable = new StoppableRunnable();
Thread thread = new Thread(stoppableRunnable, "StoppableRunnable");
thread.start();
try {
System.out.println("Putting " + Thread.currentThread().getName() + " to sleep for 5s.");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Stop request is sent after the main method sleeps for 5 seconds.
// So till then the StoppableRunnable keeps running.
System.out.println("Sending stop request to StoppableRunnable.");
stoppableRunnable.requestStop();
System.out.println("Request sent!");
}
}