-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restored old inspection threading behaviour due to #466; added a time…
…out on the background thread for sanity
- Loading branch information
Showing
2 changed files
with
33 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 30 additions & 3 deletions
33
src/main/java/org/infernus/idea/checkstyle/util/Async.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
package org.infernus.idea.checkstyle.util; | ||
|
||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.diagnostic.Logger; | ||
import com.intellij.openapi.progress.ProcessCanceledException; | ||
import com.intellij.openapi.progress.ProgressManager; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Future; | ||
|
||
public final class Async { | ||
private static final Logger LOG = Logger.getInstance(Async.class); | ||
|
||
private static final int FIFTY_MS = 50; | ||
|
||
private Async() { | ||
} | ||
|
||
@Nullable | ||
public static <T> T asyncResultOf(@NotNull final Callable<T> callable, | ||
@Nullable final T defaultValue, | ||
final long timeoutInMs) { | ||
try { | ||
return whenFinished(executeOnPooledThread(callable), timeoutInMs).get(); | ||
|
||
} catch (Exception e) { | ||
return defaultValue; | ||
} | ||
} | ||
|
||
public static <T> Future<T> executeOnPooledThread(final Callable<T> callable) { | ||
return ApplicationManager.getApplication().executeOnPooledThread(callable); | ||
} | ||
|
||
public static <T> Future<T> whenFinished(final Future<T> future) { | ||
public static <T> Future<T> whenFinished(final Future<T> future, | ||
final long timeoutInMs) { | ||
long elapsedTime = 0; | ||
while (!future.isDone() && !future.isCancelled()) { | ||
ProgressManager.checkCanceled(); | ||
waitFor(FIFTY_MS); | ||
elapsedTime += waitFor(FIFTY_MS); | ||
|
||
if (timeoutInMs >= 0 && elapsedTime >= timeoutInMs) { | ||
LOG.debug("Async task exhausted timeout of " + timeoutInMs + "ms, cancelling."); | ||
future.cancel(true); | ||
throw new ProcessCanceledException(); | ||
} | ||
} | ||
return future; | ||
} | ||
|
||
private static void waitFor(final int millis) { | ||
private static long waitFor(final int millis) { | ||
try { | ||
Thread.sleep(millis); | ||
} catch (InterruptedException ignored) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
return millis; | ||
} | ||
} |