Monitoring Threads

S

snae

Hi,

I have an app that spawns a new Thread for several identical tasks that it
needs to carry out. These tasks should never stop running and if they do
I want to take action.

I have looked at two methods of doing this. Using ThreadGroup and
assigning each of the Threads to a ThreadGroup, then looping over the
contents of the ThreadGroup to check if each Thread is running. The other
method is simply to dump each Thread object into an ArrayList and loop
over the contents of this. Either way I would have thought would do the
trick - but which way is 'better'?

The other question I have is - if a Thread stops execution, and is stored
in one of the ways above, does the object persist?. I need to be able to
restart a dead Thread, or at least get some information from that Thread
in order to start a new Thread with the correct information.

Thanks for any help.
 
S

Stefan Schulz

Hi,

I have an app that spawns a new Thread for several identical tasks that
it
needs to carry out. These tasks should never stop running and if they do
I want to take action.

I have looked at two methods of doing this. Using ThreadGroup and
assigning each of the Threads to a ThreadGroup, then looping over the
contents of the ThreadGroup to check if each Thread is running. The
other
method is simply to dump each Thread object into an ArrayList and loop
over the contents of this. Either way I would have thought would do the
trick - but which way is 'better'?

The other question I have is - if a Thread stops execution, and is stored
in one of the ways above, does the object persist?. I need to be able to
restart a dead Thread, or at least get some information from that Thread
in order to start a new Thread with the correct information.

Thanks for any help.

See the getState method (new in 1.5) for retrieving the status of a Thread

The Runnable of the Thread does indeed persist... so just store your state
here, and if you ever need to, make a new Thread with the same Runnable.
 
T

Thomas Weidenfeller

snae said:
I have an app that spawns a new Thread for several identical tasks that it
needs to carry out. These tasks should never stop running and if they do
I want to take action.

First some terminology (it pays to keep the following separate,
unfortunately Sun's documentation is not very clear about this):

A thread (lowercase) is a unit of execution. It is not an object or a class.

A Thread (uppercase) is a class in Java. Instances of this class provide
a control and monitoring mechanism for threads (lowercase). One Thread
instance controls zero or one thread.
I have looked at two methods of doing this.

The most robust would be to leave such monitoring of "must run under all
conditions" tasks to the operating system. Unfortunately Java has no API
for delegating such tasks to the OS, so if you have to use pure Java,
this is not an option.
Using ThreadGroup and
assigning each of the Threads to a ThreadGroup, then looping over the
contents of the ThreadGroup to check if each Thread is running.

There is no need to loop. activeCount() (also available as
Thread.activeCount()) returns the number of active threads in a
ThreadGroup. If the number goes down, one of your threads has died.
The other
method is simply to dump each Thread object into an ArrayList and loop
over the contents of this.

You can do this, but using the ThreadGroup seems simpler.

But in both cases, you have another issue: Deadlocked or otherwise
hanging threads. If you need to deal with these, too, you might have to
implement some heartbeat mechanism to detect this (E.g. a thread is
supposed to regularly reset a timer. If it doesn't, the timer expires,
and triggers some emergency action). You will then have the problem of
getting rid of the hanging thread (the Thread object is no great help
here, all the interesting methods have been deprecated - for very good
reasons).
The other question I have is - if a Thread stops execution, and is stored
in one of the ways above, does the object persist?

Yes, because (see above) the Thread object and the thread are not identical.

I need to be able to
restart a dead Thread, or at least get some information from that Thread
in order to start a new Thread with the correct information.

Restarting some activity, or picking up some activity which has been
previously aborted requires that you know how much progress has been
made until the problem happened. This requires that you model the state
of all the objects which are supposed to be changed by a thread in a way
that you can retrieve the state, use it to identify how fare you got,
and be able to start a new thread at a defined point where you want to
continue. Depending on the actual task, this might require a lot of
thinking, followed by a lot of coding. Some transaction log might be of
help here, too.

/Thomas
 
S

snae

See the getState method (new in 1.5) for retrieving the status of a Thread

The Runnable of the Thread does indeed persist... so just store your state
here, and if you ever need to, make a new Thread with the same Runnable.

OK, thanks a lot - this is what I want to do. I have implemented state
for the Runnable object so storing them and monitoring this way seems to
be what I want.

The Runnable object also has several timeout conditions which cause the
continual looping of the thread should certain things fail - this ensures
that the thread cannot get hung up, there are also other conditions
that can cause the thread to stop, and require restarting. It is not
enough for me to know simply that a thread stopped, which it seems
ThreadGroup can inform me of, but I need to know which thread stopped and
retrieve some information from the Runnable associated with it to start a
new thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top