JDK-8022642 : ScheduledThreadPoolExecutor with zero corePoolSize create endlessly threads
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Affected Version: 7u4
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • Submitted: 2013-06-11
  • Updated: 2015-11-09
  • Resolved: 2015-10-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b88Resolved
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version  " 1.7.0_04 " 
Java(TM) SE Runtime Environment (build 1.7.0_04-b22)
Java HotSpot(TM) Client VM (build 23.0-b21, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

EXTRA RELEVANT SYSTEM CONFIGURATION :
Not related

A DESCRIPTION OF THE PROBLEM :
When creating ScheduledThreadPoolExecutor with corePoolSize=0 and submiiting task using scheduleAtFixedRate then executor endlessly create tasks.
A simple code below demonstrate the problem:

A ScheduledThreadPoolExecutor  is created with schedule=0.
A delayed task is  scheduled to far in the future (30 hours).
After 10 seconds of running about 38K threads where created (this hang our system)

When changing  schedule to 1 then everything is run as expected


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the simple code below.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Number of created threads should be 1

Expected output:

Time offset 0(s)] threads count=1
Time offset 1(s)] threads count=1
Time offset 2(s)] threads count=1
Time offset 3(s)] threads count=1
Time offset 4(s)] threads count=1
Time offset 5(s)] threads count=1
Time offset 6(s)] threads count=1
Time offset 7(s)] threads count=1
Time offset 8(s)] threads count=1
Time offset 9(s)] threads count=1
ACTUAL -
Thousands of threads are created per second

Output:

Time offset 0(s)] threads count=1
Time offset 1(s)] threads count=3952
Time offset 2(s)] threads count=8247
Time offset 3(s)] threads count=12755
Time offset 4(s)] threads count=17007
Time offset 5(s)] threads count=21268
Time offset 6(s)] threads count=25671
Time offset 7(s)] threads count=30016
Time offset 8(s)] threads count=34334
Time offset 9(s)] threads count=38709

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class STPE_EndlesslyCreateThreads {


    public static void main(String[] args) throws InterruptedException {



        final AtomicInteger threadsCounter = new AtomicInteger();

        ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(0, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {

                Thread t = new Thread(r);

                threadsCounter.incrementAndGet();

                return t;

            }
        });

        threadPoolExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
            }
        }, 30, 30, TimeUnit.HOURS);


        long now = System.currentTimeMillis();
        Thread.sleep(1);

        while ((System.currentTimeMillis()-now) < 10*1000) {

            System.out.println( " Time offset  "  + (System.currentTimeMillis() - now) /1000 +  " (s)] threads count= "  + threadsCounter);

            Thread.sleep(1000);
        }

        threadPoolExecutor.shutdown();
        threadPoolExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);



    }
}

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

CUSTOMER SUBMITTED WORKAROUND :
Change corePoolSize to 1
Comments
jsr166 CVS integration will be providing a test case for this, ScheduledThreadPoolExecutor/ZeroCoreThreads.java and there are also accompanying code changes to ScheduledThreadPoolExecutor.java. I don't think anyone will try to backport this to jdk7, and I already see the label 7-wnf. It is claimed that this bug is not reproducible in jdk8, but I see ZeroCoreThreads fail with current stock jdk8, so I believe there is a bug fix that could be backported to jdk8, but I'm probably not going to work on that myself.
10-10-2015

The problem isn't reproduced with jdk1.8.0-b111. However, it is reproduced with jdk1.7.0_40.
23-10-2013