public class DaemonTest {
public static void main(String[] args) {new WorkerThread().start();
try {Thread.sleep(7500);} catch (InterruptedException e) {// handle here exception}
System.out.println("Main Thread ending") ;}
}
class WorkerThread extends Thread {
public WorkerThread() {// When false, (i.e. when it's a non daemon thread),// the WorkerThread continues to run.// When true, (i.e. when it's a daemon thread),// the WorkerThread terminates when the main// thread or/and user defined thread(non daemon) terminates.setDaemon(true);}
public void run() {int count = 0;
while (true) {System.out.println("Hello from Worker "+count++);
try {sleep(5000);} catch (InterruptedException e) {// handle exception here}}}}
public class DaemonThread extends Thread {public void run() {System.out.println("Entering run method");
try {System.out.println("In run Method: currentThread() is" + Thread.currentThread());
while (true) {try {Thread.sleep(500);} catch (InterruptedException x) {}
System.out.println("In run method: woke up again");}} finally {System.out.println("Leaving run Method");}}public static void main(String[] args) {System.out.println("Entering main Method");
DaemonThread t = new DaemonThread();t.setDaemon(true);t.start();
try {Thread.sleep(3000);} catch (InterruptedException x) {}
System.out.println("Leaving main method");}
}
输出:
C:\java\thread>javac DaemonThread.java
C:\java\thread>java DaemonThreadEntering main MethodEntering run methodIn run Method: currentThread() isThread[Thread-0,5,main]In run method: woke up againIn run method: woke up againIn run method: woke up againIn run method: woke up againIn run method: woke up againIn run method: woke up againLeaving main method
C:\j2se6\thread>
public class DeamonThreadTest {
public static void main(String[] args) {
new WorkerThread(false).start(); //set it to true and false and run twice.
try {Thread.sleep(7500);} catch (InterruptedException e) {// handle here exception}
System.out.println("Main Thread ending");}}
class WorkerThread extends Thread {
boolean isDeamon;
public WorkerThread(boolean isDeamon) {// When false, (i.e. when it's a user thread),// the Worker thread continues to run.// When true, (i.e. when it's a daemon thread),// the Worker thread terminates when the main// thread terminates.this.isDeamon = isDeamon;setDaemon(isDeamon);}
public void run() {System.out.println("I am a " + (isDeamon ? "Deamon Thread" : "User Thread (none-deamon)"));
int counter = 0;
while (counter < 10) {counter++;System.out.println("\tworking from Worker thread " + counter++);
try {sleep(5000);} catch (InterruptedException e) {// handle exception here}}System.out.println("\tWorker thread ends. ");}}
result when setDeamon(true)=====================================I am a Deamon Threadworking from Worker thread 0working from Worker thread 1Main Thread ending
Process finished with exit code 0
result when setDeamon(false)=====================================I am a User Thread (none-deamon)working from Worker thread 0working from Worker thread 1Main Thread endingworking from Worker thread 2working from Worker thread 3working from Worker thread 4working from Worker thread 5working from Worker thread 6working from Worker thread 7working from Worker thread 8working from Worker thread 9Worker thread ends.
Process finished with exit code 0
1. If there are no `user treads` JVM starts terminating the program2. JVM terminates all `daemon threads` automatically without waiting when they are done3. JVM is shutdown