1
1
package threadDev ;
2
2
3
- // https://stackoverflow.com/questions/28043225/future-cancel-does-not-work
4
-
5
3
import java .util .concurrent .*;
6
4
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
+ */
7
26
public class Test2 {
8
27
public static void main (String [] args ) {
9
28
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 ;
24
52
}
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" );
35
53
}
36
54
}
37
- };
55
+ } finally {
56
+ System .out .println ("FINALLY" );
57
+ }
58
+ }
59
+ };
38
60
39
61
ExecutorService executor = Executors .newSingleThreadExecutor ();
40
62
41
63
Future <?> future = executor .submit (r );
42
64
try {
43
65
future .get (3 , TimeUnit .SECONDS );
44
66
} catch (TimeoutException e ) {
45
-
46
67
System .out .println (">>> (before) future.cancel" );
47
68
boolean c = future .cancel (true );
48
69
System .out .println (">>> (after) future.cancel" );
@@ -56,5 +77,4 @@ public void run() {
56
77
System .out .println ("END" );
57
78
}
58
79
}
59
-
60
- }
80
+ }
0 commit comments