Results 1 to 4 of 4
  1. #1
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default Restarting dead threads

    I'm trying to figure out a way of keeping my threads alive in my application. I've got a main method that keeps an ArrayList of all threads it has started, each of which must remain alive for however long the entire application is running. These threads usually only die when they encounter an exception (handled or unhandled).

    Right now, I'm keeping them alive by having the main method iterate over its ArrayList of threads and invoking their isAlive() method once every second. If it returns false, it simply instantiates a new identical thread and replaces the dead one with it.

    Though this seems to be working perfectly, this doesn't really strike me as a good solution to this problem. I'm wasting CPU time by iterating over all those threads. What else can I do besides this?
    Last edited by DC200; 11-28-2010 at 10:20 PM.

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I would prevent them from dying in the first place.
    What causes them to die?

    A simple approach would be to use a while loop like so:
    Java Code:
    while (true) {
        // main logic
    }

  3. #3
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Yes, but sometimes it's necessary to restart the threads manually due to the nature of the application.

    How can the main thread find out when a thread has completed execution so it can instantiate another thread to replace it?

  4. #4
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    It looks to me that you are trying do something similar to Thread Pool. I will recommend you read up on Java Concurrent package. It has most of the functionality built in. By design, once a thread is created and allocated a task, we should let me finish and not try to bring it alive. If a new task has come up, we should allocate a new thread to it.

    Alternatively, you can choose not to let threads die, but return to the pool and re-use those. Java concurrent package will allow you to do either effectively and with getting away with writing lots of complicated code.

Similar Threads

  1. Replies: 17
    Last Post: 07-25-2010, 07:20 PM
  2. How to rotate catalina.out without restarting tomcat
    By kzvi.kzvi.1 in forum Advanced Java
    Replies: 1
    Last Post: 03-25-2010, 06:05 PM
  3. When to use threads
    By simorgh in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-12-2010, 07:43 AM
  4. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 08:39 PM
  5. Replies: 4
    Last Post: 06-05-2007, 01:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •