Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on threads: spawning threads

 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
Thanks,
Gian Franco Casula
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gian,
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.
Corey
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
L.
 
Anil Hulikal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic