File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to distingish between user thread and daemon thread ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to distingish between user thread and daemon thread ?" Watch "How to distingish between user thread and daemon thread ?" New topic
Author

How to distingish between user thread and daemon thread ?

Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Hello,
Could anyone tell me how to distingish between user thread and daemon thread ?
Thanks in advance!
Jack
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Just invoke the method Thread.isDaemon(). If it returns true, it's a daemon thread.
Corey


SCJP Tipline, etc.
Carl Sampson
Greenhorn

Joined: Apr 09, 2003
Posts: 22
There is a method called isDaemon in the Thread class that returns true if the Thread is a daemon or false otherwise. Is that what you are looking for?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Daemon threads are created by thread.setDaemon(true) before they are started. They are intended to provide general background services to a program that are not part of it; just they are needed for the program operation. Because of that, if only daemon threads are running the program terminates. AThread.isDaemon() returns true if aThread is a daemon one.
If a thread is created and setDaemon(true) is not called on it, is a user thread. The program will not end as long as there are user threads running (and you will not call System.exit(int))


SCJP2. Please Indent your code using UBB Code
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
oh, then what is the role of user thread and daemon thread ? Any examples ?
Thanks!
Jack
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
so it's also true to say the gc runs as a daemon thread
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jack Lau:
oh, then what is the role of user thread and daemon thread ? Any examples ?
Thanks!
Jack

Well, any time you'd run an application, you're creating and using a user thread. A user thread will execute to completion (unless something unforseen stops it from doing so).
A daemon thread, on the other hand, is usually used to perform some sort of background process. Java's garbage collector is an excellent example of a daemon thread. It performs a background process and, when all of the user threads finish, it is automatically destroyed (what good is a garbage collector when nothing is making anything to collect, anyway?).
I hope that helps,
Corey
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
I read a book, Main thread is created when program starts. Main thread will finish when all its child threads finish. And daemon thread only exsist to serve user threads.
So when we run an application, what is the daemon thread do ? Is daemon thread must exsist in an application ?
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
when a GUI application, AWT thread is created to monitor the user-GUI interaction. Could anyone tell me if the AWT thread is not die, does the main thread die ?
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to distingish between user thread and daemon thread ?