Skip to content

Commit 46fd7ba

Browse files
committed
make future.cancel work, via Thread.currentThread().isInterrupted()
1 parent d01e3d6 commit 46fd7ba

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

src/main/java/threadDev/Test2.java

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,69 @@
11
package threadDev;
22

3-
// https://stackoverflow.com/questions/28043225/future-cancel-does-not-work
4-
53
import java.util.concurrent.*;
64

5+
/**
6+
* future cancel fixing (modified by gpt)
7+
*
8+
* - boolean c = future.cancel(true);
9+
*
10+
*
11+
* 1. Check for Interruption in the Loop:
12+
* - Within the infinite loop, I added a check if (Thread.currentThread().isInterrupted())
13+
* to break the loop if the thread is interrupted.
14+
*
15+
* 2. Thread Interruption Handling:
16+
* - When future.cancel(true) is called, it interrupts the thread running the task.
17+
* The check within the loop ensures that the thread stops executing when it is interrupted.
18+
*
19+
* 3. Executor Shutdown:
20+
* - The executor.shutdown() is still called in the finally block
21+
* to ensure that the executor is properly shut down,
22+
* but now the thread running the task will properly exit when interrupted.
23+
*
24+
*
25+
*/
726
public class Test2 {
827
public static void main(String[] args) {
928

10-
Runnable r =
11-
new Runnable() {
12-
@Override
13-
public void run() {
14-
try {
15-
while (!Thread.currentThread().isInterrupted()) {
16-
for (; ; ) {
17-
System.out.println(
18-
"--> Thread name : "
19-
+ Thread.currentThread().getName()
20-
+ ", id = "
21-
+ Thread.currentThread().getId());
22-
System.out.println("i = ");
23-
}
29+
Runnable r = new Runnable() {
30+
@Override
31+
public void run() {
32+
try {
33+
while (!Thread.currentThread().isInterrupted()) {
34+
int iteration = 0;
35+
for (; ; ) {
36+
System.out.println(
37+
"--> Thread name : "
38+
+ Thread.currentThread().getName()
39+
+ ", id = "
40+
+ Thread.currentThread().getId() + ", iteration = " + iteration);
41+
iteration += 1;
42+
/**
43+
* NOTE !!! how we quit iteration when future.cancel(true) is called
44+
*
45+
* Thread Interruption Handling:
46+
* • When future.cancel(true) is called, it interrupts the thread running the task. The check within the loop ensures that the thread stops executing when it is interrupted.
47+
*
48+
*/
49+
if (Thread.currentThread().isInterrupted()) {
50+
System.out.println("Thread interrupted, exiting...");
51+
break;
2452
}
25-
26-
// for (; ; ) {
27-
// System.out.println("i = ");
28-
// }
29-
// for (int i = 0; i < 10; i++) {
30-
// System.out.println("i = " + i);
31-
// }
32-
33-
} finally {
34-
System.out.println("FINALLY");
3553
}
3654
}
37-
};
55+
} finally {
56+
System.out.println("FINALLY");
57+
}
58+
}
59+
};
3860

3961
ExecutorService executor = Executors.newSingleThreadExecutor();
4062

4163
Future<?> future = executor.submit(r);
4264
try {
4365
future.get(3, TimeUnit.SECONDS);
4466
} catch (TimeoutException e) {
45-
4667
System.out.println(">>> (before) future.cancel");
4768
boolean c = future.cancel(true);
4869
System.out.println(">>> (after) future.cancel");
@@ -56,5 +77,4 @@ public void run() {
5677
System.out.println("END");
5778
}
5879
}
59-
60-
}
80+
}

0 commit comments

Comments
 (0)