Skip to content

Commit 18b374e

Browse files
committed
Merge pull request #196 from qq254963746/develop
FIX Interruptible 在 jdk7 以上api改变了的bug
2 parents 027103b + 8d4546f commit 18b374e

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lts-tasktracker/src/main/java/com/lts/tasktracker/runner/JobRunnerDelegate.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.PrintWriter;
1818
import java.io.StringWriter;
19+
import java.util.concurrent.atomic.AtomicBoolean;
1920

2021
/**
2122
* Job Runner 的代理类,
@@ -35,7 +36,7 @@ public class JobRunnerDelegate implements Runnable {
3536
private TaskTrackerMonitor monitor;
3637
private Interruptible interruptor;
3738
private JobRunner curJobRunner;
38-
private boolean interrupted = false;
39+
private AtomicBoolean interrupted = new AtomicBoolean(false);
3940

4041
public JobRunnerDelegate(TaskTrackerAppContext appContext,
4142
JobWrapper jobWrapper, RunnerCallback callback) {
@@ -48,8 +49,7 @@ public JobRunnerDelegate(TaskTrackerAppContext appContext,
4849
appContext.getRemotingClient(), appContext);
4950
monitor = (TaskTrackerMonitor) appContext.getMonitor();
5051

51-
this.interruptor = new Interruptible() {
52-
@Override
52+
this.interruptor = new InterruptibleAdapter() {
5353
public void interrupt() {
5454
JobRunnerDelegate.this.interrupt();
5555
}
@@ -61,7 +61,7 @@ public void run() {
6161
try {
6262
blockedOn(interruptor);
6363
if (Thread.currentThread().isInterrupted()) {
64-
interruptor.interrupt();
64+
((InterruptibleAdapter)interruptor).interrupt();
6565
}
6666

6767
LtsLoggerFactory.setLogger(logger);
@@ -126,15 +126,16 @@ public void run() {
126126
}
127127

128128
private void interrupt() {
129-
interrupted = true;
130-
129+
if(!interrupted.compareAndSet(false, true)){
130+
return;
131+
}
131132
if (this.curJobRunner != null && this.curJobRunner instanceof InterruptibleJobRunner) {
132133
((InterruptibleJobRunner) this.curJobRunner).interrupt();
133134
}
134135
}
135136

136137
private boolean isInterrupted() {
137-
return this.interrupted;
138+
return this.interrupted.get();
138139
}
139140

140141
private void monitor(Action action) {
@@ -161,4 +162,13 @@ private static void blockedOn(Interruptible interruptible) {
161162
sun.misc.SharedSecrets.getJavaLangAccess().blockedOn(Thread.currentThread(), interruptible);
162163
}
163164

165+
public abstract class InterruptibleAdapter implements Interruptible {
166+
// for > jdk7
167+
public void interrupt(Thread thread) {
168+
interrupt();
169+
}
170+
171+
public abstract void interrupt();
172+
}
173+
164174
}

lts-tasktracker/src/test/java/com/lts/tasktracker/interrupter/InterruptRead.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
abstract class InterruptSupport {
1010
private volatile boolean interrupted = false;
11-
private Interruptible interruptor = new Interruptible() {
11+
private Interruptible interruptor = new InterruptRead.InterruptibleAdapter() {
12+
1213
public void interrupt() {
1314
interrupted = true;
1415
InterruptSupport.this.interrupt(); // 位置3
@@ -20,7 +21,7 @@ public final boolean execute() throws InterruptedException {
2021
blockedOn(interruptor); // 位置1
2122
System.out.println("=======1");
2223
if (Thread.currentThread().isInterrupted()) { // 立马被interrupted
23-
interruptor.interrupt();
24+
((InterruptRead.InterruptibleAdapter)interruptor).interrupt();
2425
System.out.println("=======2");
2526
}
2627
// 执行业务代码
@@ -91,8 +92,16 @@ public void run() {
9192
};
9293
t.start();
9394
// 先让Read执行3秒
94-
Thread.sleep(3000);
95+
Thread.sleep(30000);
9596
// 发出interrupt中断
96-
t.interrupt();
97+
// t.interrupt();
98+
}
99+
100+
public static abstract class InterruptibleAdapter implements Interruptible{
101+
public void interrupt(Thread thread) {
102+
interrupt();
103+
}
104+
105+
public abstract void interrupt();
97106
}
98107
}

0 commit comments

Comments
 (0)