From bceec81254bd11a28d7abf959881c7242b94545f Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Wed, 23 Dec 2015 01:48:35 +0100 Subject: [PATCH] Allow cancellation of queing FindBugsJob --- .../src/de/tobject/findbugs/FindBugsJob.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/eclipsePlugin/src/de/tobject/findbugs/FindBugsJob.java b/eclipsePlugin/src/de/tobject/findbugs/FindBugsJob.java index fdc5d854f4d..9402ead46ac 100644 --- a/eclipsePlugin/src/de/tobject/findbugs/FindBugsJob.java +++ b/eclipsePlugin/src/de/tobject/findbugs/FindBugsJob.java @@ -19,6 +19,7 @@ package de.tobject.findbugs; import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; @@ -123,12 +124,14 @@ public IStatus run(IProgressMonitor monitor) { if (DEBUG) { FindbugsPlugin.log("Acquiring analysisSem"); } - analysisSem.acquire(); - acquired = true; - if (DEBUG) { - FindbugsPlugin.log("Acquired analysisSem"); + + acquired = acquireAnalysisPermitUnlessCancelled(monitor); + if (acquired) { + if (DEBUG) { + FindbugsPlugin.log("Acquired analysisSem"); + } } - if(monitor.isCanceled()){ + if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } } @@ -155,4 +158,20 @@ public IStatus run(IProgressMonitor monitor) { } return Status.OK_STATUS; } + + /** + * Acquires an analysis permit unless first cancelled. + * + * @return {@code true} if permit has been acquired, {@code false} if + * cancellation was observed and permit has not been acquired + */ + private static boolean acquireAnalysisPermitUnlessCancelled(IProgressMonitor monitor) throws InterruptedException { + do { + if (analysisSem.tryAcquire(1, TimeUnit.SECONDS)) { + return true; + } + } while (!monitor.isCanceled()); + + return false; + } }