27

Possible Duplicate:
What is Daemon thread in java
When are daemon threads useful?

I am confused with the difference between user threads and daemon threads in Java.

Can you tell me:

  1. What's the difference between user threads and daemon threads in Java?
  2. In which situation daemon thread will be used? Can you give me some examples?
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
lichengwu
  • 4,277
  • 6
  • 29
  • 42

3 Answers3

25

a JVM will exit once the last non-jvm thread terminates. this means that if any of the threads you create is still running, the jvm will not shutdown. daemon threads are threads that do not prevent the JVM from shutting down. normally you'd use them for some background tasks which you dont want keeping your application up if the user requested it to shut down.

also, your question was already asked (and answered) here - What is Daemon thread in Java?

some common (from personal experience) use cases for daemon threads might include

  • background threads that poll remote systems for status changes
  • background work threads (things like sending out email notifications, snmp, whatever)
  • custom timer threads meant to perform scheduled maintainance
Community
  • 1
  • 1
radai
  • 23,949
  • 10
  • 71
  • 115
5

2nd Question :

Daemon threads get automatically terminated when all normal threads have been terminated.

You can use daemon threads to do some housekeeping or cleaning in your application. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. GC thread is a daemon thread.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
  • Why my programs stop when GC happens and why system tells that GC occures only when low memory happens? – Val Jan 11 '13 at 06:55
  • This is a separate question candidate for SO. Any way coming to your question. GC browses all active objects so usually application execution put on hold when GC runs to ensure that it gets to lookup objects in consistent state. If application runs along with GC then objects might be in in-consistent state. And yeah, usually GC happens when JVM feels it needs more free memory at its disposal. – rai.skumar Jan 11 '13 at 07:01
  • So application threads are put in sleep mode when GC starts running. – rai.skumar Jan 11 '13 at 07:05
  • When your GC thread is active only when others are sleeping, it means that you do not need the GC thread. This is not multithreading at all. Threads are needed for runnint processes _concurrently_. I mean that GC is a bad daemon example. You can optimize it out. – Val Jan 11 '13 at 07:23
  • Don't agree with your comment.. may be you can ask a separate question. – rai.skumar Jan 11 '13 at 07:28
2

A user thread is a thread that is created by the application (user), and, in most cases, a daemon thread is created by the Java VM to serve the user threads. The VM differentiates between threads, being user or daemon, when a user thread exits. In the event that a user thread exits the VM will check to find out if there are any other user threads running. If there are it will schedule the next thread (user or daemon). If there are no other user threads running, and only daemon threads, that instance of the VM will also exit.

The difference between these two types of threads is straightforward: If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.

code_fish
  • 3,381
  • 5
  • 47
  • 90
  • When invoke `System.exit(0)` the main thread and user thread will be stop, but how will JVM deal with deamon thread? Ignore? Stop? or Kill? – lichengwu Jan 11 '13 at 06:57
  • 1
    @lichengwu - system.exit() will stop everything right in its tracks. the difference is in a more graceful exit - like what happend when your main() method ends. – radai Jan 11 '13 at 07:03
  • @radai +1. Will JVM stop user thread first and then the deamon threads when JVM exit? – lichengwu Jan 11 '13 at 07:06
  • @lichengwu - i dont think they make any guarantees as to order. if you intend to write your own shutdown logic dont use System.exit() until after you've properly shutdown everything – radai Jan 11 '13 at 07:09
  • @radai well, jvm stop not only include system.exit. Do you know will the jvm stop user thread and deamon thread in order or not when closing JVM? – lichengwu Jan 11 '13 at 07:18