This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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 ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Threads Again.  RSS feed

Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have some doubts about Daemon Thread.
1) When I try to print from the run() method of a daemon thread nothing is printed out. Why?.
2) Take a look at the code below. Here there are two threads
of MyTT and one of them is Daemon. But i's value is printed out. How?

class MyTT extends Thread
int i=0;
boolean run = true;
public void run()
System.out.println("i = " + i);
} catch(InterruptedException r)
if(i > 10)
run = false;
public class DaemonThreadDemo2
public static void main(String a[])
MyTT t1 = new MyTT();
/*MyTT t2 = new MyTT(); // line 1.
t2.start(); */ //line 2.

If the comments on line 1 and 2 were removed then value of i is printed out.
Could some one explain me this?
Over to you paul and Bharatesh.
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, friend
I am not a guru but try to help you.
It is undertandable as u understand what a Daemon Thread is. The daemon thread will vanish and dead after the main thread's death.Moreover, you know, the main thread is always excuted first although you call daemon.start() which means daemon thread is ready to run and isnot scheduled to run by VM schedulor. So the main thread is running and dead after the statement t1=start(). when main thread is dead, the daemon thread still is assigned time to run and just is ready to run, so it is also dead since main thread is dead.
so when you create the 2nd thread from main thread, since the second thread have long time sleep which gives daemon thread chance to run, so you can see daemon thread print numbers.
But, your example is dangours. you maybe cannot see anything even you start another thread, bcoz in your code, nobody can gurantee the another thread can finish at first, then the main thread finish. We donot know when schedulor will assign time to child thread, sometimes, main thread finishs first, then daemon is dead, you can just see the child thread is running alone.
I am not sure I have explained it clearly and correctly, if there are error and ambiguous, please point them out.Thanks

[This message has been edited by George Toronto (edited December 31, 2000).]
vadiraj vd
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow !!! That's great. Now I know why I got the output if I start one more non-daemon thread with daemon one. You know George, sometimes I got the first print message as i = 1 in my program.
Quite interresting right ?
Thanks anyway,
There's a lot of I in J.
[This message has been edited by vadiraj vd (edited January 01, 2001).]
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!