• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Threads????

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyOne,
class Test implements Runnable
public void run(){
System.out.println("XXX");
}
public static void main(String s[])
{
Thread t=new Thread(new Test());
System.out.println(t.getName());
t.setDaemon(true);
t.start();
System.out.println("Is t Daemon? "+t.isDaemon());
}
}
my confusion is why the above code is giving different results everytime i run it?like the following results:
1)Thread-0
Is t Daemon? true
2)Thread-0
Is t Daemon? true
XXX
3)Thread-0
XXX
Is t Daemon? true
According to me,the result should always be the first option.Because according to APIs the JVM exits when the only threads running are all daemon threads.
kindly throw light on this confusion.thanx in advance
rajashree.

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i think u know that main is itself a thread and it depends on the system which threads gets the priority. So either u have to Sync. them to get the same result all the time or put one of them to sleep prior the printing.
i hope i'm right if not please feel free to correct.
thanx.
Jennifer.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you comment setDaemon(true), then the results are consistent
Thread-0
Is t Daemon? false
XXX
Userthread - main thread
Child thread - t
Daemon will be alive until all the child threads are done executing. Therefore, though main() ends quickly the program(Daemon) doesnt... since "t" is still alive
Daemon thread sole purpose is to server user threads. Its only when there are no more user/child threads left, the program terminates
So this brings us a question of setting the daemon flag.
When "t" aka child thread is set as daemon, "t" is no longer a child thread inheriting main-thread's status. Therefore the entire application will end when the user thread( main thread) dies

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program results show perfectly the behaviour of threads.
Forget for a moment the fact that one is daemon and recognize that sometimes the thread begins before the printing sentence, and in other ocasions after it. You have even seen a case where the progam ended before the beginning of the execution of the thread.
Now, in the last two results the task of the thread was so minimal that it ended before the termination of the program. Thus there is no contradiction with your statement.
For watching how a daemon thread doesn't prevent the program termination place the thread whithin an infinite loop. Compare the behavior of the program. Besides try not setting the thread to daemon. Be ready to push crtl-c ;-)
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
The program results show perfectly the behaviour of threads.
Forget for a moment the fact that one is daemon and recognize that sometimes the thread begins before the printing sentence, and in other ocasions after it. You have even seen a case where the progam ended before the beginning of the execution of the thread.
Now, in the last two results the task of the thread was so minimal that it ended before the termination of the program. Thus there is no contradiction with your statement.
For watching how a daemon thread doesn't prevent the program termination place the thread whithin an infinite loop. Compare the behavior of the program. Besides try not setting the thread to daemon. Be ready to push crtl-c ;-)



Jose, Thankx for the response.
The outputs are consistent when the child-thread is not set to
daemon thread. Here again the task for thread is minimal but
outputs are :
1. User/Main thread
2. Child thread's run method
Abnormality output is when the child-thread becomes Daemon?
I am curious to learn more about this setting.
Could you please share your comments/thoughts..
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic