• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to distingish between user thread and daemon thread ?

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Could anyone tell me how to distingish between user thread and daemon thread ?
Thanks in advance!
Jack
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just invoke the method Thread.isDaemon(). If it returns true, it's a daemon thread.
Corey
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, then what is the role of user thread and daemon thread ? Any examples ?
Thanks!
Jack
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic