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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
There are a number of ways you can do this:
How to implement Idea #5? Well, one way is to first create an interface:
then create the following class:
and then each of your Threads will extend
NotifyingThreadand instead of implementingrun()it will implementdoRun(). 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 ThreadCompleteListenerand immediately after creating each Thread add itself to the list of listeners:then, as each Thread exits, your
notifyOfThreadCompletemethod will be invoked with the Thread instance that just completed (or crashed).Note that better would be to
implements Runnablerather thanextends ThreadforNotifyingThreadas extending Thread is usually discouraged in new code. But I’m coding to your question. If you change theNotifyingThreadclass to implementRunnablethen you have to change some of your code that manages Threads, which is pretty straightforward to do.