Results 1 to 4 of 4
Thread: Restarting dead threads
- 11-28-2010, 10:02 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
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.
- 11-29-2010, 01:26 AM #2
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 }
- 11-29-2010, 01:36 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
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?
- 12-03-2010, 10:09 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
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
-
Java Question Need Help Restarting the Values of Variables from Random Numbers So Out
By JavaStudent1990 in forum New To JavaReplies: 17Last Post: 07-25-2010, 07:20 PM -
How to rotate catalina.out without restarting tomcat
By kzvi.kzvi.1 in forum Advanced JavaReplies: 1Last Post: 03-25-2010, 06:05 PM -
When to use threads
By simorgh in forum Threads and SynchronizationReplies: 2Last Post: 02-12-2010, 07:43 AM -
GUI and Threads
By rp181 in forum Threads and SynchronizationReplies: 1Last Post: 10-10-2009, 08:39 PM -
without restarting the websphere server i need to connect a newly created database
By satya in forum Enterprise JavaBeans (EJB)Replies: 4Last Post: 06-05-2007, 01:45 AM
Bookmarks