• Post Reply Bookmark Topic Watch Topic
  • New Topic

Daemon Thread  RSS feed

 
Anand Kaimal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some one please help me understand this situation!
How can the class below compile and run without implemementing runnable or extending thread?
Is the code --try { Thread.sleep(5000); } refering to the main thread?
What is supposed to happen at the end of 50000 milli sec?
import java.awt.*;

public class Daemons3 {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
Frame f = new Frame();
f.setBounds(30, 30, 400, 400);
f.setVisible(true);
}
}; // end of t1

t1.setDaemon(true);
t1.start();

try { Thread.sleep(5000); }
catch(InterruptedException e) {}
} // main()
} // Daemons3
 
Henry H
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep is a static method, hence you can call Thread.sleep().
Someone correct me if I'm wrong, but I think the thread sleeps is the main thread that JVM creates(is it a daemon thread?)
 
Anand Kaimal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanx Henry,the main thread creates a child deamon thread.

But my expectation was that the main thread would finish execution after the execution of try { Thread.sleep(5000); } which may cause JVM to exit resulting in the death of the deamon thread. But the frame remains visible on the screen even so many minutes after executing the program.
Somebody please let me understand the concept behind this piece of code.
Thanks in advance,
Anand.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand,
Thread.Sleep() always puts the 'current thread' into sleep. Here the thread that sleeps is the main thread not the thread t1!!.
To help you understand this better, I modified your code by putting a println message at the last line of the main method. When you run this program, you will notice that the message gets printed after the frame closes which means the main thread sleeps for the specified duration, prints the message and exits. That is when the daemon thread gets killed.

Hope this helps!
Ajith
 
ricky gonzalez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kept on reading it all the time, but I am still confused about what a daemon thread is. Is it the thread in JVM for things like gc, etc.?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this excellent post about daemon threads.
Ajith
 
Anand Kaimal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ajith !
Anand
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this topic to Threads and Synchronization
Ajith
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!