Thread problems

P

Paul Tomblin

I have a daemon program that listens for "events", and each time I get one
I spawn off a thread to process it. I've had a couple of occasions where
one of these threads gets "stuck", and because it uses a synchronization
object, the threads behind it get stuck as well until they restart the
daemon.

(I spawn threads rather than doing them one at a time so that I can return
to the event listener immediately.)

I'm at my wits end trying to figure out where it could possibly be getting
stuck, since it's extremely rare and has never happened in my lab. I've
added tons of debug, but of course I haven't seen it since then except at
customer sites that didn't keep the log files. So I though I'd implement
a job monitor, which does a join(MAX_RESPONSE_TIME) on a running thread,
and if the thread still isAlive(), I set a flag on it, and send it an
interrupt(), then do a join() to wait for it to finish. (The thread
periodically checks if that flag is set and exits quickly if it's set.)
At that point, I try to restart it by calling the thread's start() method,
but it never seems to start. How can I restart a thread?
 
O

Oliver Wong

Paul Tomblin said:
I set a flag on it, and send it an
interrupt(), then do a join() to wait for it to finish. (The thread
periodically checks if that flag is set and exits quickly if it's set.)
At that point, I try to restart it by calling the thread's start() method,
but it never seems to start. How can I restart a thread?

See
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#start()
<quote>
It is never legal to start a thread more than once. In particular, a thread
may not be restarted once it has completed execution.
</quote>

You wouldn't be silently swallowing exceptions, would you?

- Oliver
 
K

Knute Johnson

Paul said:
I have a daemon program that listens for "events", and each time I get one
I spawn off a thread to process it. I've had a couple of occasions where
one of these threads gets "stuck", and because it uses a synchronization
object, the threads behind it get stuck as well until they restart the
daemon.

(I spawn threads rather than doing them one at a time so that I can return
to the event listener immediately.)

I'm at my wits end trying to figure out where it could possibly be getting
stuck, since it's extremely rare and has never happened in my lab. I've
added tons of debug, but of course I haven't seen it since then except at
customer sites that didn't keep the log files. So I though I'd implement
a job monitor, which does a join(MAX_RESPONSE_TIME) on a running thread,
and if the thread still isAlive(), I set a flag on it, and send it an
interrupt(), then do a join() to wait for it to finish. (The thread
periodically checks if that flag is set and exits quickly if it's set.)
At that point, I try to restart it by calling the thread's start() method,
but it never seems to start. How can I restart a thread?

Well you can't. You can only create new threads. Once the thread has
'run off the end' it is forever finished.

If it is really getting stuck because of a synchronization problem, I
think I would fix that before I tried anything else.
 
P

Paul Tomblin

In a previous article said:
If it is really getting stuck because of a synchronization problem, I
think I would fix that before I tried anything else.

I don't think it is a synchronization problem. I think it's something
like a jdbc call that's getting stuck. There is one synchronization lock,
and if one thread gets it and gets stuck, the other threads can't
complete.
 
P

Paul Tomblin

In a previous article said:
See
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#start()
<quote>
It is never legal to start a thread more than once. In particular, a thread
may not be restarted once it has completed execution.
</quote>

Of course I'm using Java 1.4.2, and the 1.4.2 docs don't have that
paragraph. I guess better late than never with the documentation.
You wouldn't be silently swallowing exceptions, would you?

No, I print their stack trace and return. Like I said, I'm having
problems reproducing the problem, and having problems getting customers to
give me log files when it happens. If I can detect a lock-up with this
method, then I can squirrel away some log information somewhere where it
doesn't get logrotated away.
 
A

Andrew Thompson

Paul Tomblin wrote:
...
Of course I'm using Java 1.4.2, and the 1.4.2 docs don't have that
paragraph. I guess better late than never with the documentation.

(on a similar vein) Color.WHITE was introduced in Java 1.4.
I suspect that Sun believes the universe will end if they
should actually document that with with an @since tag.. :-/

Andrew T.
 
K

Knute Johnson

Paul said:
I don't think it is a synchronization problem. I think it's something
like a jdbc call that's getting stuck. There is one synchronization lock,
and if one thread gets it and gets stuck, the other threads can't
complete.

So you need an alligator or a thread killer. And you've got two
problems, why is it sticking and how to unstick it till you figure out
why. If your threads have an expected life span in time then you can
use the alligator effectively. Just start the alligator thread when you
start the task thread and when the alligator's timer is up, kill the
task thread. And that might be the way to detect what is stuck too. I
don't know anything about jdbc calls but if they are interruptible you
can interrupt/close or whatever and try to find out exactly where it is
stuck. So this will work you need to put the synchronization lock in
the alligator's thread so you can be guaranteed that it will be released
when the time is up.
 
T

Thomas Hawtin

Andrew said:
Paul Tomblin wrote:
...

1.4.2 docs do include:
"IllegalThreadStateException - if the thread was already started."

If a method throws an exception, the throws section of the API docs is a
good place to start looking.
(on a similar vein) Color.WHITE was introduced in Java 1.4.
I suspect that Sun believes the universe will end if they
should actually document that with with an @since tag.. :-/

Well, the universe still seems to be up. In 1.6 it does have an since
tag. I believe they wrote a little program to check that they are all
present and indeed correct.

I think it's worth using the latest documentation even if you are not
targeting the latest JREs. The API docs do get improved and you get to
see what is coming up. Take for example the example in the ThreadLocal docs:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ThreadLocal.html
http://download.java.net/jdk6/docs/api/java/lang/ThreadLocal.html

And in an early Dolphin build there should be more improvements to those
docs.

Tom Hawtin
 
P

Paul Tomblin

In a previous article said:
1.4.2 docs do include:
"IllegalThreadStateException - if the thread was already started."

But that doesn't tell me if it throws if the thread has ended before you
attempt to restart it - I thought it meant if it was running. Yeah, if
you know the answer already, it's obvious, but to me "already started" was
ambiguous.
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top