1616
1717import java .io .PrintWriter ;
1818import 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}
0 commit comments