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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:02:44+00:00 2026-06-16T15:02:44+00:00

When I try to learn about Java threads, I usually come across with the

  • 0

When I try to learn about Java threads, I usually come across with the code examples with wait() and notify() in the very same class (infact nearly all of them producer-consumer examples). After googling various examples, unfortunately I could not find the the case I need which is:

  • A manager thread initially creates n number of threads (and starts them) in which a http get request is done in a sigle thread.
  • For a single worker thread it takes about 20-30 seconds to complete its life.
  • Here my manager thread must know which of workers have finished, and replaces finishing thread with a new one.

I thought about an approach like that (let n be 5):

List<Runnable> runnables = new ArrayList<Runnable>();
for(int i = 0 ; i < 5 ; i++){
    runnables.add(new MyWorker(params));
}
for(Runnable myWorker : runnables){
   myWorker.run();
}

Since wait() does not support multiple objects I cannot keep on going from here. Another solution can be implementing busy wait on manager thread that is calling some isFinished flag for each worker. But I’m not sure either this a good approach (as far as I know this is resource wasting)

  • 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-16T15:02:45+00:00Added an answer on June 16, 2026 at 3:02 pm

    There is a ready-to-use tool in Java 6 and above, in the form of the executor framework.

    Since you want to have a fixed pool of threads, your best bet is to use:

    ExecutorService service = Executors.newFixedThreadPool(5);
    

    You can then submit your Runnable instances using the .execute() method (which is defined by the Executor interface). They will be submitted to a work queue and dequeued as soon as a thread in the pool is available.

    If your individual thread-working methods return a value (ie, they implement Callable<Something>), then you can use the .submit() method (defined by ExecutorService, also an interface which extends Executor), which will return a Future<Something> from which you will .get() the computed values.

    Terminating a thread pool is done in various ways: .shutdown() is the most basic and will synchronously wait for still active threads to terminate (and prevent new jobs to be submitted).

    Javadocs: Executors, ExecutorService, ThreadPoolExecutor.

    Other link: a book you should buy for all things thread-related (does not cover Java 7’s ForkJoinPool however).

    PS: how lucky, the sample chapter of the book above (PDF) covers task execution 😉

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

Sidebar

Related Questions

I'm about to learn the java threading facility. I have 2 classes: public class
Bear with me as I try to learn more about .htaccess redirect rules -
I am doing the below test to try and learn more about LINQ to
I'm writing a simple IRC bot to try to learn more about Clojure, using
I am coming from Java and try to learn Javascript right now. Doing that
Hello I'm very new to OpenGlES and i have started to try to learn
Okay, first off, i started (trying) to learn Java about 2 days ago. im
I'm making a bingo card generator to try and learn more about WPF, and
I am trying to learn about thread and find some examples in the internet.
I'm trying to learn about java's event handlers and keep getting errors with type

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.