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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:48:03+00:00 2026-06-04T14:48:03+00:00

I started reading more about ThreadPoolExecutor from Java Doc as I am using it

  • 0

I started reading more about ThreadPoolExecutor from Java Doc as I am using it in one of my project. So Can anyone explain me what does this line means actually?- I know what does each parameter stands for, but I wanted to understand it in more general/lay-man way from some of the experts here.

ExecutorService service = new ThreadPoolExecutor(10, 10, 1000L,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10, true), new 
ThreadPoolExecutor.CallerRunsPolicy());

Updated:-
Problem Statement is:-

Each thread uses unique ID between 1 and 1000 and program has to run for 60 minutes or more, So in that 60 minutes it is possible that all the ID’s will get finished so I need to reuse those ID’s again. So this is the below program I wrote by using above executor.

class IdPool {
    private final LinkedList<Integer> availableExistingIds = new LinkedList<Integer>();

    public IdPool() {
        for (int i = 1; i <= 1000; i++) {
            availableExistingIds.add(i);
        }
    }

    public synchronized Integer getExistingId() {
        return availableExistingIds.removeFirst();
    }

    public synchronized void releaseExistingId(Integer id) {
        availableExistingIds.add(id);
    }
}


class ThreadNewTask implements Runnable {
    private IdPool idPool;

    public ThreadNewTask(IdPool idPool) {
        this.idPool = idPool;
    }

    public void run() {
        Integer id = idPool.getExistingId();
        someMethod(id);
        idPool.releaseExistingId(id);
    }

// This method needs to be synchronized or not?
    private synchronized void someMethod(Integer id) {
        System.out.println("Task: " +id);
// and do other calcuations whatever you need to do in your program
    }
}

public class TestingPool {
    public static void main(String[] args) throws InterruptedException {
        int size = 10;
        int durationOfRun = 60;
        IdPool idPool = new IdPool();   
        // create thread pool with given size
        ExecutorService service = new ThreadPoolExecutor(size, size, 500L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(size), new ThreadPoolExecutor.CallerRunsPolicy()); 

        // queue some tasks
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (durationOfRun * 60 * 1000L);

        // Running it for 60 minutes
        while(System.currentTimeMillis() <= endTime) {
            service.submit(new ThreadNewTask(idPool));
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }
}

My Questions is:- This code is right as far as the Performance is considered or not? And what else I can make it here to make it more accurate? Any help will 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-04T14:48:04+00:00Added an answer on June 4, 2026 at 2:48 pm

    [First, i apologize, this is a response to a previous answer, but i wanted formatting].

    Except in reality, you DON’T block when an item is submitted to a ThreadPoolExecutor with a full queue. The reason for this is that ThreadPoolExecutor calls the BlockingQueue.offer(T item) method which by definition is a non-blocking method. It either adds the item and returns true, or does not add (when full) and returns false. The ThreadPoolExecutor then calls the registered RejectedExecutionHandler to deal with this situation.

    From the javadoc:

    Executes the given task sometime in the future. The task may execute
    in a new thread or in an existing pooled thread. If the task cannot be
    submitted for execution, either because this executor has been
    shutdown or because its capacity has been reached, the task is handled
    by the current RejectedExecutionHandler.

    By default, the ThreadPoolExecutor.AbortPolicy() is used which throws a RejectedExecutionException from the “submit” or “execute” method of the ThreadPoolExecutor.

    try {
       executorService.execute(new Runnable() { ... });
    }
    catch (RejectedExecutionException e) {
       // the queue is full, and you're using the AbortPolicy as the 
       // RejectedExecutionHandler
    }
    

    However, you can use other handlers to do something different, such as ignore the error (DiscardPolicy), or run it in the thread which called the “execute” or “submit” method (CallerRunsPolicy). This example lets whichever thread calls the “submit” or “execute” method run the requested task when the queue is full. (this means at any given time, you could 1 additional thing running on top of what’s in the pool itself):

    ExecutorService service = new ThreadPoolExecutor(..., new ThreadPoolExecutor.CallerRunsPolicy());
    

    If you want to block and wait, you could implement your own RejectedExecutionHandler which would block until there’s a slot available on the queue (this is a rough estimate, i have not compiled or run this, but you should get the idea):

    public class BlockUntilAvailableSlot implements RejectedExecutionHandler {
      public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
         if (e.isTerminated() || e.isShutdown()) {
            return;
         }
    
         boolean submitted = false;
         while (! submitted) {
           if (Thread.currentThread().isInterrupted()) {
                // be a good citizen and do something nice if we were interrupted
                // anywhere other than during the sleep method.
           }
    
           try {
              e.execute(r);
              submitted = true;
           }
           catch (RejectedExceptionException e) {
             try {
               // Sleep for a little bit, and try again.
               Thread.sleep(100L);
             }
             catch (InterruptedException e) {
               ; // do you care if someone called Thread.interrupt?
               // if so, do something nice here, and maybe just silently return.
             }
           }
         }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started reading about ORMLite since I am interested in using it
I started reading about underscore.js today, it is a library for javascript that adds
When I first started reading about and learning ruby, I read something about the
I recently started reading about ASP.net MVC and after getting excited about the concept,
When I first started reading about Python, all of the tutorials have you use
I have recently started reading about dependency injection and it has made me rethink
Coming from the OOP I started in the last weeks to read about functional
I've started reading The C Programming Language (K&R) and I have a doubt about
After learning a bit of Scheme from SICP, I started reading The Little Schemer
I recently started reading Evans' Domain-Driven design book and started a small sample project

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.