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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:08:47+00:00 2026-05-11T16:08:47+00:00

I have an object with a method named StartDownload() , that starts three threads.

  • 0

I have an object with a method named StartDownload(), that starts three threads.

How do I get a notification when each thread has finished executing?

Is there a way to know if one (or all) of the thread is finished or is still executing?

  • 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-11T16:08:47+00:00Added an answer on May 11, 2026 at 4:08 pm

    There are a number of ways you can do this:

    1. Use Thread.join() in your main thread to wait in a blocking fashion for each Thread to complete, or
    2. Check Thread.isAlive() in a polling fashion — generally discouraged — to wait until each Thread has completed, or
    3. Unorthodox, for each Thread in question, call setUncaughtExceptionHandler to call a method in your object, and program each Thread to throw an uncaught Exception when it completes, or
    4. Use locks or synchronizers or mechanisms from java.util.concurrent, or
    5. More orthodox, create a listener in your main Thread, and then program each of your Threads to tell the listener that they have completed.

    How to implement Idea #5? Well, one way is to first create an interface:

    public interface ThreadCompleteListener {
        void notifyOfThreadComplete(final Thread thread);
    }
    

    then create the following class:

    public abstract class NotifyingThread extends Thread {
      private final Set<ThreadCompleteListener> listeners
                       = new CopyOnWriteArraySet<ThreadCompleteListener>();
      public final void addListener(final ThreadCompleteListener listener) {
        listeners.add(listener);
      }
      public final void removeListener(final ThreadCompleteListener listener) {
        listeners.remove(listener);
      }
      private final void notifyListeners() {
        for (ThreadCompleteListener listener : listeners) {
          listener.notifyOfThreadComplete(this);
        }
      }
      @Override
      public final void run() {
        try {
          doRun();
        } finally {
          notifyListeners();
        }
      }
      public abstract void doRun();
    }
    

    and then each of your Threads will extend NotifyingThread and instead of implementing run() it will implement doRun(). Thus when they complete, they will automatically notify anyone waiting for notification.

    Finally, in your main class — the one that starts all the Threads (or at least the object waiting for notification) — modify that class to implement ThreadCompleteListener and immediately after creating each Thread add itself to the list of listeners:

    NotifyingThread thread1 = new OneOfYourThreads();
    thread1.addListener(this); // add ourselves as a listener
    thread1.start();           // Start the Thread
    

    then, as each Thread exits, your notifyOfThreadComplete method will be invoked with the Thread instance that just completed (or crashed).

    Note that better would be to implements Runnable rather than extends Thread for NotifyingThread as extending Thread is usually discouraged in new code. But I’m coding to your question. If you change the NotifyingThread class to implement Runnable then you have to change some of your code that manages Threads, which is pretty straightforward to do.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As long as you wrap your user-defined types using Swig… May 11, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer Actually, decided to chime in with my own answer. This… May 11, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer The problem is in the run method of YUV4MPEGPipeParser class.… May 11, 2026 at 7:35 pm

Related Questions

In Django, given excerpts from an application animals likeso: A animals/models.py with: from django.db
I have an interface named IAuthorizationRepository with the following interface: public interface IAuthorizationRepository {
Ok. So I have a list of values, and I'd like to do something
I'm aiming to create a set of objects, each of which has a unique
I've just started to learn about tie . I have a class named Link

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.