-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
37 lines (30 loc) · 891 Bytes
/
Main.java
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
package com.codecafe.concurrency.thread.threadtermination;
class LongComputationTask implements Runnable {
@Override
public void run() {
// Intentional infinite loop to simulate a long computation task
for (; ; ) {
// Returns true if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
// You are supposed to roll back or reverse the operation in progress and stop
System.out.println("\nThread is interrupted hence stopping...");
// Terminates the loop
break;
}
System.out.print("T");
}
}
}
public class Main {
public static void main(String[] args) {
Thread th = new Thread(new LongComputationTask());
th.start();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
// th.stop(); never use this
th.interrupt();
}
}