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 / April 2007

Tip: Looking for answers? Try searching our database.

terminiating a thread when main throws an error

Thread view: 
christopher@dailycrossword.com - 01 Apr 2007 20:03 GMT
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
Karl Uppiano - 01 Apr 2007 20:38 GMT
If you mark any new threads you create as "daemon" (before you start them),
the VM will exit when the main thread dies.
christopher@dailycrossword.com - 01 Apr 2007 21:24 GMT
> 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!
Karl Uppiano - 01 Apr 2007 21:38 GMT
>> If you mark any new threads you create as "daemon" (before you start
>> them),
[quoted text clipped - 4 lines]
> thread that has a life of its own.  Had it backwards as usual.
> Cheers!

According to this
(http://en.wikipedia.org/wiki/Daemon_%28computer_software%29) 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...
christopher@dailycrossword.com - 01 Apr 2007 23:12 GMT
> <christop...@dailycrossword.com> wrote in message
>
[quoted text clipped - 26 lines]
>
> 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!


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



©2009 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.