We can call setDaemon(boolean) method to change a thread to
daemon thread before the thread starts.
xxxxxxxxxx
public class JavaDaemonTest {
4
5
public static void main(String args[]) {
6
// Create runnable action for daemon
7
Runnable daemonRunner = new Runnable() {
8
public void run() {
9
// Repeat forever
10
while (true) {
11
System.out.println("I'm a Daemon.");
12
// Sleep for half a second
13
try {
14
Thread.sleep(500);
15
16
} catch (InterruptedException ignored) {
17
18
}
19
}
20
}
21
};
22
// Create and start daemon thread
23
Thread daemonThread = new Thread(daemonRunner);
24
daemonThread.setDaemon(true);
25
daemonThread.start();
26
// Sleep for five seconds
27
try {
28
Thread.sleep(5000);
29
30
} catch (InterruptedException ignored) {
31
}
32
System.out.println("Done.");
33
}
34
}