forked from Kotlin-Polytech/FromKotlinToJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
30 lines (23 loc) · 676 Bytes
/
Copy pathMain.java
File metadata and controls
30 lines (23 loc) · 676 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
package part4.stopthread;
public class Main {
private static class Stop {
private boolean requested = false;
void request() {
requested = true;
}
boolean isRequested() {
return requested;
}
}
private static Stop stop = new Stop();
public static void main(String[] args) throws InterruptedException {
Thread backgroundThread = new Thread(() -> {
int i = 0;
while (!stop.isRequested()) i++;
System.out.println("Background thread finished");
});
backgroundThread.start();
Thread.sleep(1000);
stop.request();
}
}