What is a daemon thread in Java?

2018-12-31 02:35发布

Can anybody tell me what daemon threads are in Java?

24条回答
其实,你不懂
2楼-- · 2018-12-31 03:09

Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our programm code. JVM doesn't terminates unless all the user thread terminate.

查看更多
谁念西风独自凉
3楼-- · 2018-12-31 03:12

Daemon thread in Java are those thread which runs in background and mostly created by JVM for performing background task like Garbage collection and other house keeping tasks.

Points to Note :

  1. Any thread created by main thread, which runs main method in Java is by default non daemon because Thread inherits its daemon nature from the Thread which creates it i.e. parent Thread and since main thread is a non daemon thread, any other thread created from it will remain non-daemon until explicitly made daemon by calling setDaemon(true).

  2. Thread.setDaemon(true) makes a Thread daemon but it can only be called before starting Thread in Java. It will throw IllegalThreadStateException if corresponding Thread is already started and running.

Difference between Daemon and Non Daemon thread in Java :

1) JVM doesn't wait for any daemon thread to finish before existing.

2) Daemon Thread are treated differently than User Thread when JVM terminates, finally blocks are not called, Stacks are not unwounded and JVM just exits.

查看更多
有味是清欢
4楼-- · 2018-12-31 03:12

In Java, Daemon Threads are one of the types of the thread which does not prevent Java Virtual Machine (JVM) from exiting. The main purpose of a daemon thread is to execute background task especially in case of some routine periodic task or work. With JVM exits, daemon thread also dies.

By setting a thread.setDaemon(true), a thread becomes a daemon thread. However, you can only set this value before the thread start.

查看更多
明月照影归
5楼-- · 2018-12-31 03:13

Daemon threads are threads that run in the background as long as other non-daemon threads of the process are still running. Thus, when all of the non-daemon threads complete, the daemon threads are terminated. An example for the non-daemon thread is the thread running the Main. A thread is made daemon by calling the setDaemon() method before the thread is started

For More Reference : Daemon thread in Java

查看更多
初与友歌
6楼-- · 2018-12-31 03:14

Definition of Daemon (Computing):

A background process that handles requests for services such as print spooling and file transfers, and is dormant when not required.

—— Source: English by Oxford Dictionaries

What is Daemon thread in Java?

  • Daemon threads can shut down any time in between their flow, Non-Daemon i.e. user thread executes completely.
  • Daemon threads executes at a low priority.
  • Daemon threads are threads that run intermittently in the background as long as other non-daemon threads are running.
  • When all of the non-daemon threads complete, daemon threads terminates automatically.
  • Daemon threads are service providers for user threads running in the same process.
  • The JVM does not care about daemon threads to complete when in Running state, not even finally block also let execute. JVM do give preference to non-daemon threads that is created by us.
  • Daemon threads acts as services in Windows.
  • The JVM stops the daemon threads when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement, for example, a monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped.
查看更多
谁念西风独自凉
7楼-- · 2018-12-31 03:15

Java has a special kind of thread called daemon thread.

  • Very low priority.
  • Only executes when no other thread of the same program is running.
  • JVM ends the program finishing these threads, when daemon threads are the only threads running in a program.

What are daemon threads used for?

Normally used as service providers for normal threads. Usually have an infinite loop that waits for the service request or performs the tasks of the thread. They can’t do important jobs. (Because we don't know when they are going to have CPU time and they can finish any time if there aren't any other threads running. )

A typical example of these kind of threads is the Java garbage collector.

There's more...

  • You only call the setDaemon() method before you call the start() method. Once the thread is running, you can’t modify its daemon status.
  • Use isDaemon() method to check if a thread is a daemon thread or a user thread.
查看更多
登录 后发表回答