From 8d4546f4a1f311a647d76a8a611dd62319dd3df0 Mon Sep 17 00:00:00 2001 From: hugui <254963746@qq.com> Date: Tue, 23 Feb 2016 12:34:08 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20Interruptible=20=E5=9C=A8=20jdk7=20?= =?UTF-8?q?=E4=BB=A5=E4=B8=8Aapi=E6=94=B9=E5=8F=98=E4=BA=86=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tasktracker/runner/JobRunnerDelegate.java | 24 +++++++++++++------ .../interrupter/InterruptRead.java | 17 +++++++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lts-tasktracker/src/main/java/com/lts/tasktracker/runner/JobRunnerDelegate.java b/lts-tasktracker/src/main/java/com/lts/tasktracker/runner/JobRunnerDelegate.java index ba6f13723..9bae5e208 100644 --- a/lts-tasktracker/src/main/java/com/lts/tasktracker/runner/JobRunnerDelegate.java +++ b/lts-tasktracker/src/main/java/com/lts/tasktracker/runner/JobRunnerDelegate.java @@ -16,6 +16,7 @@ import java.io.PrintWriter; import java.io.StringWriter; +import java.util.concurrent.atomic.AtomicBoolean; /** * Job Runner 的代理类, @@ -35,7 +36,7 @@ public class JobRunnerDelegate implements Runnable { private TaskTrackerMonitor monitor; private Interruptible interruptor; private JobRunner curJobRunner; - private boolean interrupted = false; + private AtomicBoolean interrupted = new AtomicBoolean(false); public JobRunnerDelegate(TaskTrackerAppContext appContext, JobWrapper jobWrapper, RunnerCallback callback) { @@ -48,8 +49,7 @@ public JobRunnerDelegate(TaskTrackerAppContext appContext, appContext.getRemotingClient(), appContext); monitor = (TaskTrackerMonitor) appContext.getMonitor(); - this.interruptor = new Interruptible() { - @Override + this.interruptor = new InterruptibleAdapter() { public void interrupt() { JobRunnerDelegate.this.interrupt(); } @@ -61,7 +61,7 @@ public void run() { try { blockedOn(interruptor); if (Thread.currentThread().isInterrupted()) { - interruptor.interrupt(); + ((InterruptibleAdapter)interruptor).interrupt(); } LtsLoggerFactory.setLogger(logger); @@ -126,15 +126,16 @@ public void run() { } private void interrupt() { - interrupted = true; - + if(!interrupted.compareAndSet(false, true)){ + return; + } if (this.curJobRunner != null && this.curJobRunner instanceof InterruptibleJobRunner) { ((InterruptibleJobRunner) this.curJobRunner).interrupt(); } } private boolean isInterrupted() { - return this.interrupted; + return this.interrupted.get(); } private void monitor(Action action) { @@ -161,4 +162,13 @@ private static void blockedOn(Interruptible interruptible) { sun.misc.SharedSecrets.getJavaLangAccess().blockedOn(Thread.currentThread(), interruptible); } + public abstract class InterruptibleAdapter implements Interruptible { + // for > jdk7 + public void interrupt(Thread thread) { + interrupt(); + } + + public abstract void interrupt(); + } + } diff --git a/lts-tasktracker/src/test/java/com/lts/tasktracker/interrupter/InterruptRead.java b/lts-tasktracker/src/test/java/com/lts/tasktracker/interrupter/InterruptRead.java index 051c7dc7b..b7a6983c9 100644 --- a/lts-tasktracker/src/test/java/com/lts/tasktracker/interrupter/InterruptRead.java +++ b/lts-tasktracker/src/test/java/com/lts/tasktracker/interrupter/InterruptRead.java @@ -8,7 +8,8 @@ abstract class InterruptSupport { private volatile boolean interrupted = false; - private Interruptible interruptor = new Interruptible() { + private Interruptible interruptor = new InterruptRead.InterruptibleAdapter() { + public void interrupt() { interrupted = true; InterruptSupport.this.interrupt(); // 位置3 @@ -20,7 +21,7 @@ public final boolean execute() throws InterruptedException { blockedOn(interruptor); // 位置1 System.out.println("=======1"); if (Thread.currentThread().isInterrupted()) { // 立马被interrupted - interruptor.interrupt(); + ((InterruptRead.InterruptibleAdapter)interruptor).interrupt(); System.out.println("=======2"); } // 执行业务代码 @@ -91,8 +92,16 @@ public void run() { }; t.start(); // 先让Read执行3秒 - Thread.sleep(3000); + Thread.sleep(30000); // 发出interrupt中断 - t.interrupt(); +// t.interrupt(); + } + + public static abstract class InterruptibleAdapter implements Interruptible{ + public void interrupt(Thread thread) { + interrupt(); + } + + public abstract void interrupt(); } } \ No newline at end of file