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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:29:22+00:00 2026-05-15T11:29:22+00:00

I have a java application where the main-thread starts 2 other threads. If one

  • 0

I have a java application where the main-thread starts 2 other threads.
If one of these threads terminates, the main-thread may start another thread depending on the result of the terminated thread.

Example:
The main-thread creates 2 threads: A and B. Thread A will load a picture and thread B will load another picture. If A terminates and loaded the picture successfully a new Thread C will be created which does some other stuff and so on.

How can i do this? I do not want to use busy waiting in the main thread and check every 100ms if one of the two threads has finished.
I think i cannot use a thread pool because the number of active threads (in this case A and B) will vary extremely and it’s the main-threads dicision to create a new thread or not.

This is rough sketch of the “busy waiting” solution:

public class TestThreads {
 private class MyThread extends Thread {
  volatile boolean done = false;
  int steps;

  @Override
  public void run() {
   for (int i=0; i<steps; i++) {
    System.out.println(Thread.currentThread().getName() + ": " + i);
    try {
     Thread.sleep(1000);
    } catch (InterruptedException exc) {  }
   }
   done = true;
   synchronized (this) {
    notify();
   }
  }

  public void waitFor(long ms) {
   synchronized (this) {
    try {
     wait(ms);
    } catch (InterruptedException exc) {  }    
   }
  }
 }

 public void startTest() {
  MyThread a = new MyThread();
  a.steps = 6;
  a.start();

  MyThread b = new MyThread();
  b.steps = 3;
  b.start();

  while (true) {
   if (!a.done) {
    a.waitFor(100);
    if (a.done) {
     System.out.println("C will be started, because A is done.");
    }
   }

   if (!b.done) {
    b.waitFor(100);
    if (b.done) {
     System.out.println("C will be started, because B is done.");
    }
   }

   if (a.done && b.done) {
    break;
   }
  }
 }

 public static void main(String[] args) {
  TestThreads test = new TestThreads();
  test.startTest();
 }
}
  • 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-05-15T11:29:22+00:00Added an answer on May 15, 2026 at 11:29 am

    This sounds like a classic case for using a ThreadPoolExecutor for performing the tasks concurrently, and wrapping it with an ExecutorCompletionService, for collecting the results as they arrive.

    For example, assuming that tasks contains a set of tasks to execute in parallel, each returning a String value when it terminates, the code to process the results as they become available can be something like:

    List<Callable<String>> tasks = ....;
    Executor ex = Executors.newFixedThreadPool(10);
    ExecutorCompletionService<String> ecs = new ExecutorCompletionService<String>(ex);
    for (Callable<String> task : tasks) 
        ecs.submit(task);
    for(int i = 0; i < tasks.size(); i++) {
        String result = ecs.take().get();
        //Do something with result
    }
    

    If you include the identity of the task as a part of the returned value, then you can make decisions depending on the completion order.

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

Sidebar

Ask A Question

Stats

  • Questions 427k
  • Answers 427k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is that it uses C-style strings instead of… May 15, 2026 at 12:49 pm
  • Editorial Team
    Editorial Team added an answer The "propput" part should look like this: [propput, id(0x00000065)] HRESULT… May 15, 2026 at 12:49 pm
  • Editorial Team
    Editorial Team added an answer Create an XML file according to the RSS standard. If… May 15, 2026 at 12:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.