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

Busy loop in ThreadPoolExecutor.getTask for ScheduledThreadPoolExecutor

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8u20, 8u25, 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.8.0_20"
        Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
        Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        14.0.0 Darwin Kernel Version 14.0.0

        A DESCRIPTION OF THE PROBLEM :
        When ThreadPoolExecutor.keepAliveTime is 0 and workQueue is a DelayQueue (i.e. the executor is a ScheduledThreadPoolExecutor), the loop in the private getTask method spins without yielding or timed waiting. The problematic piece of code is:

        Runnable r = timed ?
                            workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                            workQueue.take();



        ADDITIONAL REGRESSION INFORMATION:
        java version "1.8.0_20"
        Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
        Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        This code reproduces the problem:

        public static void main(String[] args) throws InterruptedException {
                ScheduledExecutorService e = Executors.newScheduledThreadPool(0);
                e.schedule(() -> {}, 60, TimeUnit.SECONDS);
                Thread.sleep(60000);
            }

        For 60 seconds, "top" shows 100% CPU on one core.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        While waiting for the next task, the worker thread should use blocking timed wait.
        ACTUAL -
        No blocking wait, 100% CPU spinning.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package test;

        import java.util.concurrent.*;

        public class TPTest {

            public static void main(String[] args) throws InterruptedException {
                ScheduledExecutorService e = Executors.newScheduledThreadPool(0);
                e.schedule(() -> {}, 60, TimeUnit.SECONDS);
                Thread.sleep(60000);
            }

        }

        ---------- END SOURCE ----------

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                martin Martin Buchholz
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: