1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java: Detecting when thread stops.

Discussion in 'HTML, Graphics & Programming' started by adam3223, 15 Apr 2007.

  1. adam3223

    Gangster

    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.
     
  2. Una

    Wise Guy

    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: 16 Apr 2007
  3. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Somewhere

    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.
     
  4. Visage

    PermaBanned

    Joined: 13 Jan 2005

    Posts: 10,708

    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.
     
  5. adam3223

    Gangster

    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.
     
  6. Eriedor

    Wise Guy

    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.
     
  7. Chrisss

    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?
     
  8. Visage

    PermaBanned

    Joined: 13 Jan 2005

    Posts: 10,708

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

    Wise Guy

    Joined: 26 Nov 2004

    Posts: 2,471

    Location: Reading / Lake District

    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: 16 Apr 2007
  10. adam3223

    Gangster

    Joined: 22 Jul 2004

    Posts: 230

    Location: Nottingham, UK

    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.
     
  11. daven1986

    Mobster

    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?
     
  12. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Somewhere

    .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:
    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: 16 Apr 2007
  13. daven1986

    Mobster

    Joined: 29 Oct 2005

    Posts: 3,298

    True, it would seem silly not to.