A daemon thread is a thread whose execution state is not evaluated when the JVM decides if it should stop or not. The JVM stops when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement for example monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped:
--
The example application above terminates even though the daemon thread is still running in its endless while loop.
--
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Example { | |
private static class MyDaemonThread extends Thread { | |
public MyDaemonThread() { | |
setDaemon(true); | |
} | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
Thread.sleep(1); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
public static void main(String[] args) throws InterruptedException { | |
Thread thread = new MyDaemonThread(); | |
thread.start(); | |
} | |
} |
The example application above terminates even though the daemon thread is still running in its endless while loop.
No comments:
Post a Comment