Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Q on threads: spawning threads

 
blacksmith
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
 
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
 
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 ]
 
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
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
 
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.
 
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic