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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:32:24+00:00 2026-06-08T06:32:24+00:00

I am developing a program that can send http requests to fetch documents. I

  • 0

I am developing a program that can send http requests to fetch documents.
I have fill a queue with all the requests items:

Queue<RequestItem> requestItems = buildRequest4Docs();

Then,

int threadNum = requestItems.size();
        //ExecutorService exs = Executors.newFixedThreadPool(threadNum);

        for (int i = 0; i < threadNum; i++) {
            ResponseInterface response = new CMSGOResponse();
            RequestTask task = new RequestTask(requestItems.poll(), this, response);
            task.run();
            //exs.execute(new RequestTask(requestItems.poll(), this, response));
        }
        //exs.shutdown();

I am confused here, in the for loop,does the tasks run simultaneously? Or the tasks run one by one?

Thanks!

  • 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-08T06:32:25+00:00Added an answer on June 8, 2026 at 6:32 am

    In the way you got it now the tasks will be executed one by one. If you uncomment the code you got now as comments and comment the lines RequestTask task = new RequestTask(requestItems.poll(), this, response); and task.run(); you will get a concurrent execution.

    So for the concurrent execution it has to look like this:

    int threadNum = requestItems.size();
    ExecutorService exs = Executors.newFixedThreadPool(threadNum);
    
    for (int i = 0; i < threadNum; i++) {
        ResponseInterface response = new CMSGOResponse();
        exs.execute(new RequestTask(requestItems.poll(), this, response));
    }
    exs.shutdown();
    while (! exs.isTerminated()) {
        try {
            exs.awaitTermination(1L, TimeUnit.DAYS);
        }
        catch (InterruptedException e) {
            // you may or may not care here, but if you truly want to
            // wait for the pool to shutdown, just ignore the exception
            // otherwise you'll have to deal with the exception and
            // make a decision to drop out of the loop or something else.
        }
    }
    

    In addition to that I suggest, that you do not bind the amount of threads created with the ExecutorService to the amount of task you got to work. Connecting it to the amount of processors of the host system is usually a better method. To get the amount of processors use: Runtime.getRuntime().availableProcessors()

    And in the executor service initialized like this you put the items of your queue. But that works nicely without fetching the total size, rather by polling the Queue until it does not return additional data.

    The final result of my proposals could look like this:

    final int threadNum = Runtime.getRuntime().availableProcessors();
    final ExecutorService exs = Executors.newFixedThreadPool(threadNum);
    
    while (true) {
        final RequestItem requestItem = requestItems.poll();
        if (requestItem == null) {
            break;
        }
        final ResponseInterface response = new CMSGOResponse(); 
        exs.execute(new RequestTask(requestItem , this, response));
    }
    exs.shutdown();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# game program that i'm developing. it uses sound samples and
I have a C program that I'm developing using Ubuntu 11.10 (Linux version 3.0.0-12-generic-pae
I'm currently developing a percussion tutorial program. The program requires that I can determine
I have been developing a program lately that compiles and runs a C++ Program
I am developing a java web program that can manage distant ldap entries, we
I am developing a program that will bootstrap another program, so as a result
I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This
I'm developing a program that would require huge amount of memory, and I want
I'm developing a program that makes some floating points calculations. Is there any way
I'm developing a PyQt program that will soon switch from an xml type backend

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.