• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

join() in thread...continues....  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imin my previous post i had put a question on join in threads. Corey had given very nice explaination to it.(http://www.coderanch.com/t/245443/java-programmer-SCJP/certification/join-threads)

This question is the continuation to it.
We can set the spawned threads as daemon thread it means that when the main thread dies all spawned thread should terminate and program should terminate.

in the same code (which i had put previously, if i put the t2 thread as setDaemon(true) then the program output is totally different.
I tried to figure out the flow....but it is going out of my scope...


please see the code below....



TIA,

Saiprasad
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Since the newly created threads (spawned as anonymous threads)
now are daemon threads, you would expect them not to print
anything but 21 and 11 respectively if they get a chance to
run. This is because, due to the join, the first spawned thread
waits for the main thread to finish and the second spawned thread
waits for the first spawned thread. But, since the main thread
finishing implies the termination of the program and thus of all
daemon threads, the daemon threads in the given code wouldn't
seem to have the chance to invoke the code on line 3 (see code
below)

But, the contrary seems true, because by running the code on
my machine the two daemon threads get the chance to invoke the
code on line 3.

At first sight I thought this was weird: main terminates and
daemon threads still produce output
But I remembered an issue which might explain this behaviour as
well. You know that you can't call the start() method twice on
a thread instance with a terminated run() method. Well, if you
invoke the start() method a second time while the thread is
dying then you will get a runtime error complaining, but if you
let the thread die and call start() after sleeping a bit then
nothing happens and there is no runtime error. This is because
in the former case the dying thread hasn't managed to settle all
it's status information.

I believe that in your example the same is happening. The daemon
threads just produce output, because the main thread takes longer
to change to the dead status when it is actually terminated. If
you change the code as follows (the newly spawned threads always
sleep for a relatively long period, see line 1 and 2):



then you will see that the daemon threads won't print anything,
because the main thread's status settles to dead while the daemons
are sleeping and subsequently the daemons are terminated because
there is no running non-daemon thread anymore.

HTH, cheers,

Gian Franco
[ June 06, 2004: Message edited by: Gian Franco Casula ]
 
Hey cool! They got a blimp! But I have a tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!