Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Thread problems

Thread view: 
Paul Tomblin - 19 Jul 2006 22:42 GMT
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?

Signature

Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
Illiterate?  Write for help!

Oliver Wong - 19 Jul 2006 22:48 GMT
> 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
Paul Tomblin - 19 Jul 2006 23:38 GMT
In a previous article, "Oliver Wong" <owong@castortech.com> said:
>"Paul Tomblin" <ptomblin+netnews@xcski.com> wrote in message
>> but it never seems to start.  How can I restart a thread?
[quoted text clipped - 5 lines]
>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.

Signature

Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
"Maybe if your vcr is still blinking 12:00 you shouldn't be using Linux."
-- Slashdot poster

Andrew Thompson - 20 Jul 2006 00:00 GMT
..
> 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.
Thomas Hawtin - 20 Jul 2006 13:34 GMT
> ...
>> 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.

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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Paul Tomblin - 20 Jul 2006 13:54 GMT
In a previous article, Thomas Hawtin <usenet@tackline.plus.com> said:
>> ...
>>> 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.
>
>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.

Signature

Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
Medication did wonders for me, Dave. Perhaps it could for you, if a
crowbar and half a pound of axle grease counts as medication.
             -- Red Drag Diva

Knute Johnson - 19 Jul 2006 22:56 GMT
> 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
[quoted text clipped - 15 lines]
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Paul Tomblin - 19 Jul 2006 23:33 GMT
In a previous article, Knute Johnson <nospam@rabbitbrush.frazmtn.com> 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.

Signature

Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
"Go go Gadget kernel compile!" - Chris "Saundo" Saunderson

Knute Johnson - 20 Jul 2006 05:12 GMT
> In a previous article, Knute Johnson <nospam@rabbitbrush.frazmtn.com> said:
>> If it is really getting stuck because of a synchronization problem, I
[quoted text clipped - 4 lines]
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Andrew Thompson - 19 Jul 2006 22:56 GMT
..
> ..How can I restart a thread?

Threads cannot be restarted.  You need to create a new
Thread each time.

HTH

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.