Optimization. ThreadLocal.get() is expensive. Let's check it no often that once in...
authorMaxim Shafirov <max@jetbrains.com>
Fri, 17 Sep 2021 20:27:09 +0000 (23:27 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Sat, 18 Sep 2021 10:44:36 +0000 (10:44 +0000)
GitOrigin-RevId: 51080935d24c777da1928b8b4c0513d29f59183a

platform/platform-tests/testSrc/com/intellij/openapi/progress/CancellationPropagationTest.kt
platform/util/src/com/intellij/openapi/progress/Cancellation.java

index c9377471b260be0d5f5acc315f3c123a9add3831..8ff6ee8be562a8e2bed72ad91721791cb563ca50 100644 (file)
@@ -206,7 +206,10 @@ class CancellationPropagationTest {
       childFuture2 = service.submit {
         lock.up()
         childFuture2CanFinish.waitUp()
-        ProgressManager.checkCanceled()
+        repeat(5) {
+          ProgressManager.checkCanceled()
+          Thread.sleep(50)
+        }
       }
       lock.up()
     }
index c2c02197b6f6cae1b22e0430759bcf8931dd6853..1db157c74624c418295819cb55425928852b1c2a 100644 (file)
@@ -14,6 +14,7 @@ import org.jetbrains.annotations.VisibleForTesting;
 public final class Cancellation {
 
   private static final ThreadLocal<Job> ourJob = new ThreadLocal<>();
+  private static long lastTimeChecked = -1L;
 
   private Cancellation() { }
 
@@ -29,6 +30,10 @@ public final class Cancellation {
   }
 
   public static void checkCancelled() {
+    long now = System.nanoTime();
+    if (now - lastTimeChecked < 50000000) return;
+    lastTimeChecked = now;
+
     Job job = ourJob.get();
     if (job != null) {
       JobKt.ensureActive(job);