This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to distingish between user thread and daemon thread ?

 
Jack Lau
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Could anyone tell me how to distingish between user thread and daemon thread ?
Thanks in advance!
Jack
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just invoke the method Thread.isDaemon(). If it returns true, it's a daemon thread.
Corey
 
Carl Sampson
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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))
 
Jack Lau
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, then what is the role of user thread and daemon thread ? Any examples ?
Thanks!
Jack
 
preeti khane
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it's also true to say the gc runs as a daemon thread
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic