Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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?
CODE
------

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

-----------
END OF CODE
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.
Regadrs
--------
vadiraj
 
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
regds
George


[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,
Regards
-------
vadiraj
------------------
****************
There's a lot of I in J.
****************
[This message has been edited by vadiraj vd (edited January 01, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!