File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q on threads: spawning threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q on threads: spawning threads" Watch "Q on threads: spawning threads" New topic

Q on threads: spawning threads

Gian Franco
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
I don't understand the following:
If the code spawning threads is not put within try-catch
block in the main() method, the main thread would finish executing
before the child thread; however, the program would run until the
child thread completes

Can anyone please explain how this should be implemented and why
it does what it does?
Gian Franco Casula

"Eppur si muove!"
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
My GUESS is that the try-catch block would somehow (without blocking the application code from executing past it) block the main thread from exiting until all threads creating within it terminate.
My experience with threading is limited though so I may be wrong in this

Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59
Hi Gian Franco Casula,
I think it is not true. I have written a short class:

The result is as follows:
C:\__Work\Java\MyFiles>java d.D1
main is done.
Sam is done.
As You can see, the main Thread finished as the first one.
[ May 06, 2004: Message edited by: Gabriel Forro ]

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
There appears to be more than this than you're letting on. Just putting a block of code within a try/catch block isn't going to prevent it from moving forward. Perhaps there was something else the author mentioned, such as a join method invocation, or something else. It seems that your statement is taken very "out of context" and, in order to make any sort of intelligible reply, we need to be given more information.

SCJP Tipline, etc.
Gian Franco
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
It supposedly has to come from
Programmer's Guide to Java Certification
by Mughal according to some notes I got
from JavaRanch topic: Ashraf Fouad's concentrated guide to SCJP
I don't have the book now so I can't check
the context of the statment.
Anyone an idea?
Thanks for you help,
Gian Franco
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
As far as I know the running program will wait only for user threads. If in the main method you start a deamon thread, your program won't wait for it. I don't see what the try/catch block has to do with anything. It all comes down to user vs. deamon thread.
Maybe it's all out of context as Corey said ...
Anil Hulikal

Joined: Apr 02, 2004
Posts: 29
This is probably in ref. with the execution of user threads vs daemon threads, as explained in section 9.2 of "A Programmer's Guide to Java Certification Second Edition" by K Mughal and R Rasmussen
If it does not violate any copyright laws, I can quote the paragraph.

Just stay focused.
I agree. Here's the link:
subject: Q on threads: spawning threads
It's not a secret anymore!