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 9121255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:49:10+00:00 2026-06-17T05:49:10+00:00

I am using the Executors framework in Java to create thread pools for a

  • 0

I am using the Executors framework in Java to create thread pools for a multi-threaded application, and I have a question related to performance.

I have an application which can work in realtime or non-realtime mode. In case it’s realtime, I’m simply using the following:

THREAD_POOL = Executors.newCachedThreadPool();

But in case it’s not realtime, I want the ability to control the size of my thread pool.
To do this, I’m thinking about 2 options, but I don’t really understand the difference, and which one would perform better.

Option 1 is to use the simple way:

THREAD_POOL = Executors.newFixedThreadPool(threadPoolSize);

Option 2 is to create my own ThreadPoolExecutor like this:

RejectedExecutionHandler rejectHandler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
    try {
        executor.getQueue().put(r);
    } catch (Exception e) {}
}
};          
THREAD_POOL = new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10000), rejectHandler);

I would like to understand what is the advantage of using the more complex option 2, and also if I should use another data structure than LinkedBlockingQueue? Any help would be appreciated.

  • 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-06-17T05:49:11+00:00Added an answer on June 17, 2026 at 5:49 am

    Looking at the source code you’ll realize that:

    Executors.newFixedThreadPool(threadPoolSize);
    

    is equivalent to:

    return new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
    

    Since it doesn’t provide explicit RejectedExecutionHandler, default AbortPolicy is used. It basically throws RejectedExecutionException once the queue is full. But the queue is unbounded, so it will never be full. Thus this executor accepts inifnite1 number of tasks.

    Your declaration is much more complex and quite different:

    • new LinkedBlockingQueue<Runnable>(10000) will cause the thread pool to discard tasks if more than 10000 are awaiting.

    • I don’t understand what your RejectedExecutionHandler is doing. If the pool discovers it cannot put any more runnables to the queue it calls your handler. In this handler you… try to put that Runnable into the queue again (which will fail in like 99% of the cases block). Finally you swallow the exception. Seems like ThreadPoolExecutor.DiscardPolicy is what you are after.

      Looking at your comments below seems like you are trying to block or somehow throttle clients if tasks queue is too large. I don’t think blocking inside RejectedExecutionHandler is a good idea. Instead consider CallerRunsPolicy rejection policy. Not entirely the same, but close enough.

    To wrap up: if you want to limit the number of pending tasks, your approach is almost good. If you want to limit the number of concurrent threads, the first one-liner is enough.

    1 – assuming 2^31 is infinity

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a question about the reasonableness of using entity framework only with stored
I have recently started testing for my android application using Instrumentation framework. I have
If I create a fixed size thread pool with 10 threads in java using
currently i create dynamic database using CreateDatabase() method in Entity Framework. it executes sucessfully
I have submitted a task using executors and I need it to stop after
I have a ruby application that executes ant as a subprocess using backtick. This
Using play framework 1.2.4 with scala. I have few play jobs that looks like
I've been developing a PHP application using the Zend framework for awhile on a
I want to test my REST-Service using the RestEasy Client Framework. In my application
Im working on a large multi-module project which uses an internal framework as one

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.