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.
 

Una

Una

Associate
Joined
26 Nov 2004
Posts
2,471
Location
Reading / Lake District
Hmm maybe I am missing something but can't you have the threads cleanup before they terminate? Would be easier if you try to tell me what your doing instead (sounds like your trying to threadpool?) because its not the way I would go about it.
 
Last edited:
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
Una said:
Hmm maybe I am missing something but can't you have the threads cleanup before they terminate? Would be easier if you try to tell me what your doing instead (sounds like your trying to threadpool?) because its not the way I would go about it.
That's what I'd do. Just have the thread do its work and clean itself up when it's done. Unless, for some reason, the cleaning up has to be done on the main thread, of course.
 
Permabanned
Joined
13 Jan 2005
Posts
10,708
Inquisitor said:
That's what I'd do. Just have the thread do its work and clean itself up when it's done. Unless, for some reason, the cleaning up has to be done on the main thread, of course.

Yup. If a thread is using resources that require initialisation/cleanup, then cleanup should be the reposnibility of the same thread that did the initialisation.
 
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
Joined
1 Feb 2006
Posts
1,868
Location
Reading
I don't code java but i'm just gonna throw this out anyway :p

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.
 
Permabanned
Joined
19 Aug 2006
Posts
1,604
I don't know much about threads but couldn't you just have a boolean for each thread which gets set to true just before the thread has finished, and then you could monitor each boolean and know which threads are still busy etc?
 
Permabanned
Joined
13 Jan 2005
Posts
10,708
adam3223 said:
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.

Why do you need to keep a list of references to your threads?
 

Una

Una

Associate
Joined
26 Nov 2004
Posts
2,471
Location
Reading / Lake District
Chrisss said:
I don't know much about threads but couldn't you just have a boolean for each thread which gets set to true just before the thread has finished, and then you could monitor each boolean and know which threads are still busy etc?

I know what your getting at here... there is an isAlive() method for the threads. You could have a garbage collecting thread, which sleeps for a certain period of time, wakes up and then polls the ArrayList and checks to see which threads are dead and thus removes them from the List. However I was trying to think up a better solution....

Why is there any reason for keeping a reference in a list to each of your threads?
 
Last edited:
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.
 
Soldato
Joined
29 Oct 2005
Posts
3,298
maybe make another thread which waits until a thread terminates, when a thread terminates it then notifies this extra thread which will perform a cleanup?
 
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
adam3223 said:
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.
.NET events fire on the same thread though. If the main thread must always be active (which I'm assuming it must), then you won't be able to join it to another thread, as that'll cause it to block.

Ultimately, afaik, the only way you can get one thread to do something as soon as another has finished is either to block on some kind of wait handle (which obviously isn't an option here), or to check each thread's status periodically.

Remember that threads execute in a linear fashion; if the main thread is doing something, you can't just force it to stop and do something else when a thread has finished its work. It must either be waiting for that in the first place or checking every now and then, as I said.

Again, why does the cleaning up have to be done on the main thread?

edit:
daven1986 said:
maybe make another thread which waits until a thread terminates, when a thread terminates it then notifies this extra thread which will perform a cleanup?
This would be the other way of doing it of course, but then this raises the question: why not just do it on the worker thread itself?
 
Last edited:
Back
Top Bottom