Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:48:36+00:00 2026-05-31T01:48:36+00:00

I’m looking to have a ThreadPoolExecutor where I can set a corePoolSize and a

  • 0

I’m looking to have a ThreadPoolExecutor where I can set a corePoolSize and a maximumPoolSize and what happens is the queue would hand off task immediately to the thread pool and thus create new threads until it reaches the maximumPoolSize then start adding to a queue.

Is there such a thing? If not, are there any good reason it doesn’t have such a strategy?

What I want essentially is for tasks to be submitted for execution and when it reaches a point where it is essentially going to get ‘worst’ performance from having too many threads (by setting maximumPoolSize), it would stop adding new threads and work with that thread pool and start queuing, then if the queue is full it rejects.

And when load comes back down, it can start dismantling threads that are unused back to the corePoolSize.

This makes more sense to me in my application than the ‘three general strategies’ listed in http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:48:37+00:00Added an answer on May 31, 2026 at 1:48 am

    Note: these implementations are somewhat flawed and non-deterministic. Please read the entire answer and the comments before using this code.

    How about creating a work queue that rejects items while the executor is below the maximum pool size, and starts accepting them once the maximum has been reached?

    This relies on the documented behavior:

    “If a request cannot be queued, a new thread is created unless this
    would exceed maximumPoolSize, in which case, the task will be
    rejected.”

    public class ExecutorTest
    {
        private static final int CORE_POOL_SIZE = 2;
        private static final int MAXIMUM_POOL_SIZE = 4;
        private static final int KEEP_ALIVE_TIME_MS = 5000;
    
        public static void main(String[] args)
        {
            final SaturateExecutorBlockingQueue workQueue = 
                new SaturateExecutorBlockingQueue();
    
            final ThreadPoolExecutor executor = 
                new ThreadPoolExecutor(CORE_POOL_SIZE, 
                        MAXIMUM_POOL_SIZE, 
                        KEEP_ALIVE_TIME_MS, 
                        TimeUnit.MILLISECONDS, 
                        workQueue);
    
            workQueue.setExecutor(executor);
    
            for (int i = 0; i < 6; i++)
            {
                final int index = i;
                executor.submit(new Runnable()
                {
                    public void run()
                    {
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
    
                        System.out.println("Runnable " + index 
                                + " on thread: " + Thread.currentThread());
                    }
                });
            }
        }
    
        public static class SaturateExecutorBlockingQueue 
            extends LinkedBlockingQueue<Runnable>
        {
            private ThreadPoolExecutor executor;
    
            public void setExecutor(ThreadPoolExecutor executor)
            {
                this.executor = executor;
            }
    
            public boolean offer(Runnable e)
            {
                if (executor.getPoolSize() < executor.getMaximumPoolSize())
                {
                    return false;
                }
                return super.offer(e);
            }
        }
    }
    

    Note: Your question surprised me because I expected your desired behavior to be the default behavior of a ThreadPoolExecutor configured with a corePoolSize < maximumPoolSize. But as you point out, the JavaDoc for ThreadPoolExecutor clearly states otherwise.


    Idea #2

    I think I have what is probably a slightly better approach. It relies on the side-effect behavior coded into the setCorePoolSize method in ThreadPoolExecutor. The idea is to temporarily and conditionally increase the core pool size when a work item is enqueued. When increasing the core pool size, the ThreadPoolExecutor will immediately spawn enough new threads to execute all the queued (queue.size()) tasks. Then we immediately decrease the core pool size, which allows the thread pool to shrink naturally during future periods of low activity. This approach is still not fully deterministic (it is possible for the pool size to grow above max pool size, for example), but I think it is in almost all cases it is better than the first strategy.

    Specifically, I believe this approach is better than the first because:

    1. It will reuse threads more often
    2. It will not reject execution as a result of a race
    3. I would like to mention again that the first approach causes the thread pool to grow to its maximum size even under very light use. This approach should be much more efficient in that regard.

    –

    public class ExecutorTest2
    {
        private static final int KEEP_ALIVE_TIME_MS = 5000;
        private static final int CORE_POOL_SIZE = 2;
        private static final int MAXIMUM_POOL_SIZE = 4;
    
        public static void main(String[] args) throws InterruptedException
        {
            final SaturateExecutorBlockingQueue workQueue = 
                new SaturateExecutorBlockingQueue(CORE_POOL_SIZE, 
                        MAXIMUM_POOL_SIZE);
    
            final ThreadPoolExecutor executor = 
                new ThreadPoolExecutor(CORE_POOL_SIZE, 
                        MAXIMUM_POOL_SIZE, 
                        KEEP_ALIVE_TIME_MS, 
                        TimeUnit.MILLISECONDS, 
                        workQueue);
    
            workQueue.setExecutor(executor);
    
            for (int i = 0; i < 60; i++)
            {
                final int index = i;
                executor.submit(new Runnable()
                {
                    public void run()
                    {
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
    
                        System.out.println("Runnable " + index 
                                + " on thread: " + Thread.currentThread()
                                + " poolSize: " + executor.getPoolSize());
                    }
                });
            }
    
            executor.shutdown();
    
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        }
    
        public static class SaturateExecutorBlockingQueue 
            extends LinkedBlockingQueue<Runnable>
        {
            private final int corePoolSize;
            private final int maximumPoolSize;
            private ThreadPoolExecutor executor;
    
            public SaturateExecutorBlockingQueue(int corePoolSize, 
                    int maximumPoolSize)
            {
                this.corePoolSize = corePoolSize;
                this.maximumPoolSize = maximumPoolSize;
            }
    
            public void setExecutor(ThreadPoolExecutor executor)
            {
                this.executor = executor;
            }
    
            public boolean offer(Runnable e)
            {
                if (super.offer(e) == false)
                {
                    return false;
                }
                // Uncomment one or both of the below lines to increase
                // the likelyhood of the threadpool reusing an existing thread 
                // vs. spawning a new one.
                //Thread.yield();
                //Thread.sleep(0);
                int currentPoolSize = executor.getPoolSize();
                if (currentPoolSize < maximumPoolSize 
                        && currentPoolSize >= corePoolSize)
                {
                    executor.setCorePoolSize(currentPoolSize + 1);
                    executor.setCorePoolSize(corePoolSize);
                }
                return true;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.