Java: Detecting when thread stops.

Associate
Joined
22 Jul 2004
Posts
230
Location
Nottingham, UK
My main thread creates new threads in a while loop as they are needed. To store the threads i keep them in a arraylist so i can reference them later.

But i want the main thread to clean up any threads which have stoped, and i can't think of a reasonable way todo this:

I can't use join, as the main thread doesn't know which will stop first, and i don't want it blocked whilst waiting for it anyway.

Ideally i'd want the terminating thread to call a cleanup function in the main thread (as a reference is passed to it to interact with the main thread whilst its running). But i can't terminate the thread from a method with is being called by it.
In c# i could use a delegate to make a nonblocking cross thread call to do this, but i don't think this is possible in java.

Any ideas would be appreciated.
 
Associate
OP
Joined
22 Jul 2004
Posts
230
Location
Nottingham, UK
I'm mainly talking about the cleanup of the actual thread itself. I am right in assuming if the thread stops it won't be garbage collected if something still holds a reference to it. (it doesn't matter about the intialisations inside the thread, they are aleardy cleaned up when it terminates.)


At the moment i have an arraylist which contains classes called activethread. This class contains a reference to the actual thread class, and other information such as what time it was created etc. This information could be moved into the thread itself, but it would still result in the list being populated with stopped threads.

When a thread stops i want the main thread to remove its reference from the arraylist, and perhaps print out what time it had finshed etc.

Regarding threadpooling, isn't that creating a certain amount of threads to share a large amount of work? My threads are created upon demand, and i don't wish to keep them afterwards.
 
Associate
OP
Joined
22 Jul 2004
Posts
230
Location
Nottingham, UK
eriedor said:
Could you not use the same concept of updating controls created by another thread? For example have the thread raise an event when it is finished, in that event have it raise an asynchronous invoke that will cause the same event to be called from the main thread which can now remove reference of the thread that finished from the arraylist thus allowing garbage collection.
The specification states i must keep track of all active threads and notify the user if one terminates.

This is the ideal approach. Coming for a C# background i would prefer to set up an event which signals the main thread asynchronously that the child thread is closing. The main thread could then wait using the join function until the thread has stopped and remove it from the list.
I've done a bit of research into how java implements events, and i think i can implement this to solve the problem.

Regarding creating a thread to continuously check the state of the other threads. I thought about that before, and i didn't like it due to its inefficiency.
 
Back
Top Bottom