terminiating a thread when main throws an error

C

christopher

Greetings!
I am sure there is a simple answer to how to terminate a thread when
main throws an error.

if it matters, all main does is instantiate its object so I can do non-
static stuff. The thread implements runnable and the constructor for
the object that contains 'main' spins the thread like this:
sThread=new SocketThread(controlSocket);
Thread t = new Thread(sThread);
t.start();

the SocketThread checks a local variable every 2 seconds or so and has
a method for setting that variable so the thread will gracefully
terminate when main ends properly.

If I have an uncaught runtime error the thread keeps the JVM alive
indefinitely.

Do I need to wrap the constructor in a 'try' block (or main?), or is
there a better way?

BTW, tried searching for this but the terms are too generic :<(
Thanx!
-- clh
 
K

Karl Uppiano

If you mark any new threads you create as "daemon" (before you start them),
the VM will exit when the main thread dies.
 
C

christopher

If you mark any new threads you create as "daemon" (before you start them),
the VM will exit when the main thread dies.


thank you
that would be the old assumption thing where I think of a daemon as a
thread that has a life of its own. Had it backwards as usual.
Cheers!
 
K

Karl Uppiano

thank you
that would be the old assumption thing where I think of a daemon as a
thread that has a life of its own. Had it backwards as usual.
Cheers!

According to this
(http://en.wikipedia.org/wiki/Daemon_(computer_software)) a daemon is a
background process, so I guess it makes sense...

BTW, another way you could solve your original problem would be to create a
globally accessible boolean called "running", and all of your threads could
monitor that (e.g., while(running)...). Your main thread could catch any
exceptions and set running to false.

Yet another way would be for the main thread to keep references to all the
threads that it started, and interrupt them when an exception occurs. The
threads would have to monitor their interrupted status to see if they need
to exit (e.g., while(!interrupted()) ...). This latter approach would work
even if they were blocked in a wait. Unfortunately, you cannot interrupt a
blocking socket read (at least, not the last time I tried).

Plenty of ways to skin this cat...
 
C

christopher

According to this
(http://en.wikipedia.org/wiki/Daemon_(computer_software)) a daemon is a
background process, so I guess it makes sense...

BTW, another way you could solve your original problem would be to create a
globally accessible boolean called "running", and all of your threads could
monitor that (e.g., while(running)...). Your main thread could catch any
exceptions and set running to false.

Yet another way would be for the main thread to keep references to all the
threads that it started, and interrupt them when an exception occurs. The
threads would have to monitor their interrupted status to see if they need
to exit (e.g., while(!interrupted()) ...). This latter approach would work
even if they were blocked in a wait. Unfortunately, you cannot interrupt a
blocking socket read (at least, not the last time I tried).

Plenty of ways to skin this cat...

I just don't like the look of a try block on main -- violates my
personal esthetic I guess! setDaemon(true) worked as advertized. As
far as the socket thing, I set a time out on the ServerSocket so
accept() only blocks for a coupla seconds, then I loop on an
"isRunning" boolean. It's only to ensure only one process is running
at a time, so nothing fancy needed.

Cheers!
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top