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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:07:54+00:00 2026-06-03T06:07:54+00:00

So in Java concurrency, there is the concept of a task which is really

  • 0

So in Java concurrency, there is the concept of a task which is really any implementing Runnable or Callable (and, more specifically, the overridden run() or call() method of that interface).

I’m having a tough time understanding the relationship between:

  • A task (Runnable/Callable); and
  • An ExecutorService the task is submitted to; and
  • An underlying, concurrent work queue or list structure used by the ExecutorService

I believe the relationship is something of the following:

  • You, the developer, must select which ExecutorService and work structure best suits the task at hand
  • You initialize the ExecutorService (say, as a ScheduledThreadPool) with the underlying structure to use (say, an ArrayBlockingQueue) (if so, how?!?!)
  • You submit your task to the ExecutorService which then uses its threading/pooling strategy to populate the given structure (ABQ or otherwise) with copies of the task
  • Each spawned/pooled thread now pulls copies of the task off of the work structure and executes it

First off, please correct/clarify any of the above assumptions if I am off-base on any of them!

Second, if the task is simply copied/replicated over and over again inside the underlying work structure (e.g., identical copies in each index of a list), then how do you ever decompose a big problem down into smaller (concurrent) ones? In other words, if the task simply does steps A – Z, and you have an ABQ with 1,000 of those tasks, then won’t each thread just do A – Z as well? How do you say “some threads should work on A – G, while other threads should work on H, and yet other threads should work on I – Z”, etc.?

For this second one I might need a code example to visualize how it all comes together. Thanks in advance.

  • 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-03T06:07:55+00:00Added an answer on June 3, 2026 at 6:07 am

    Your last assumption is not quite right. The ExecutorService does not pull copies of the task. The program must supply all tasks individually to be performed by the ExecutorService. When a task has finished, the next task in the queue is executed.

    An ExecutorService is an interface for working with a thread pool. You generally have multiple tasks to be executed on the pool, and each operates on a different part of the problem. As the developer, you must specify which parts of the problem each task should work on when creating it, before sending it to the ExecutorService. The results of each task (assuming they are working on a common problem) should be added to a BlockingQueue or other concurrent collection, where another thread may use the results or wait for all tasks to finish.

    Here is an article you may want to read about how to use an ExecutorService: http://www.vogella.com/articles/JavaConcurrency/article.html#threadpools

    Update: A common use of the ExecutorService is to implement the producer/consumer pattern. Here is an example I quickly threw together to get you started–it is intended for demonstration purposes only, as some details and concerns have been omitted for simplicity. The thread pool contains multiple producer threads and one consumer thread. The job being performed is to sum the numbers from 0…N. Each producer thread sums a smaller interval of numbers, and publishes the result to the BlockingQueue. The consumer thread processes each result added to the BlockingQueue.

    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class NumberCounter {
    
        private final ExecutorService pool = Executors.newFixedThreadPool(2);
        private final BlockingQueue<Integer> queue = new ArrayBlockingQueue(100);
    
        public void startCounter(int max, int workers) {
            // Create multiple tasks to add numbers. Each task submits the result
            // to the queue.
            int increment = max / workers;
            for (int worker = 0; worker < workers; worker++) {
                Runnable task = createProducer(worker * increment, (worker + 1) * increment);
                pool.execute(task);
            }
    
            // Create one more task that will consume the numbers, adding them up
            // and printing the results.
            pool.execute(new Runnable() {
    
                @Override
                public void run() {
                int sum = 0;
    
                while (true) {
                    try {
                    Integer result = queue.take();
                    sum += result;
                    System.out.println("New sum is " + sum);
                    } catch (InterruptedException e) {
                    e.printStackTrace();
                    }
                }
    
                }
            });
    
        }
    
        private Runnable createProducer(final int start, final int stop) {
            return new Runnable() {
    
                @Override
                public void run() {
                System.out.println("Worker started counting from " + start + " to " + stop);
                int count = 0;
                for (int i = start; i < stop; i++) {
                    count += i;
                    }
                    queue.add(count);
                }
    
            };
        }
    
        public static void main(String[] args) throws InterruptedException {
            NumberCounter counter = new NumberCounter();
            counter.startCounter(10000, 5);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
In Java Concurrency in Practice there is a sample that made me confused: public
I come from a Java background. I am wanting to learn more about concurrency
I start learning some java concurrency concept ans put in use. But one of
I'm studying Java Concurrency in Practice and there it is explained why the following
In Java Concurrency In Practice book (p.156), there's a statement regarding poison pill approach:
I have a java concurrency problem, it goes like this: There is a Servlet
Is there a Java thread analyzer like the concurrency analyzer in Visual Studio?
There's something odd about the implementation of the BoundedExecutor in the book Java Concurrency
I'm using below concurrency feature of Java 1.6 to execute some task offline. When

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.