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

The Archive Base Latest Questions

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

This is a simplification of my code: for(int i = 0; i < 500;

  • 0

This is a simplification of my code:

for(int i = 0; i < 500; i++) {
    someMethod(i);
}

someMethod() takes a long time to execute, so I want to use multithreading to break up the for-loop into 5 intervals of 100:

for(int i = 0; i < 100; i++) {
    someMethod(i);
}

for(int i = 100; i < 200; i++) {
    someMethod(i);
}

...

for(int i = 400; i < 500; i++) {
    someMethod(i);
}

so I can execute someMethod() concurrently for different i.

How do I use multithreading to accomplish this?

Please help! 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-08T14:40:33+00:00Added an answer on June 8, 2026 at 2:40 pm

    It is recommended to use the great ExecutorService code for situations like this. What you do is submit all of your tasks (0 to 499) into the thread pool and they will be run concurrently by the 5 threads in the pool.

    Something like the following:

    // create a thread pool with 5 workers
    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    // submit all of your jobs here
    for (int i = 0; i < 500; i++) {
        threadPool.submit(new MyJob(i));
    }
    // once we have submitted all jobs to the thread pool, it should be shutdown
    threadPool.shutdown();
    // if you need to wait for the pool you can do
    threadPool.awaitTerminatation(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    

    private static class MyJob implements Runnable {
       private int i;
       public MyJob(int i) {
           this.i = i;
       }
       public void run() {
           // do the thread stuff here
       }
    }
    

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

Sidebar

Related Questions

This is my code (simplification of a real-life problem): class Foo { public: void
This is the code (just a simplification of a real problem): <?php echo memory_get_usage()
While looking at this code (most of which has been removed for simplification of
Best explained with code: long pieceLength = Math.Pow(2,18); //simplification ... public void HashFile(string path)
Simplification of the code to express the concept: class classA { public: private: int
I have this (simplification): $(li).click(function{alert(test);}); <li> <input> </li> What's the best way to bind
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content
This code below allows me to find the word error in all my files
This is my sample code: SPWeb web = SPContext.Current.Web SPList list = web.Lists[TestList]; try
This is what I'm trying to do (this is a simplification of a real

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.