Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8308548

Interrupted java.util.concurrent.FutureTask did not throw an InterruptedException when calling the get method

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE PROBLEM :
      The thread that has been interrupted did not throw an InterruptedException when calling the get method

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1、Obtain a Future that is already in a completed state.
      2、Call the get method of this future using an interrupted thread.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Throw an InterruptedException
      ACTUAL -
      Instead of throwing an exception, a normal result was obtained.

      CUSTOMER SUBMITTED WORKAROUND :
      Modify the awaitDone method to advance the judgment of interrupts.

      private int awaitDone(boolean timed, long nanos)
              throws InterruptedException {
              long startTime = 0L;
              WaitNode q = null;
              boolean queued = false;
              for (;;) {
                  if (Thread.interrupted()) {
                      removeWaiter(q);
                      throw new InterruptedException();
                  }
                  int s = state;
                  if (s > COMPLETING) {
                      if (q != null)
                          q.thread = null;
                      return s;
                  }
                  else if (s == COMPLETING)
                      Thread.yield();
                  else if (q == null) {
                      if (timed && nanos <= 0L)
                          return s;
                      q = new WaitNode();
                  }
                  else if (!queued)
                      queued = WAITERS.weakCompareAndSet(this, q.next = waiters, q);
                  else if (timed) {
                      final long parkNanos;
                      if (startTime == 0L) {
                          startTime = System.nanoTime();
                          if (startTime == 0L)
                              startTime = 1L;
                          parkNanos = nanos;
                      } else {
                          long elapsed = System.nanoTime() - startTime;
                          if (elapsed >= nanos) {
                              removeWaiter(q);
                              return state;
                          }
                          parkNanos = nanos - elapsed;
                      }
                      if (state < COMPLETING)
                          LockSupport.parkNanos(this, parkNanos);
                  }
                  else
                      LockSupport.park(this);
              }
          }


      FREQUENCY : always


      Attachments

        Activity

          People

            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: