java thread into thread guard and non-daemon threads. When the non-threaded daemon, a daemon thread ends automatically.
Typical daemon thread is the garbage collection thread.
Example:
package threadTestSHXC;
public class MyThread extends Thread{
private int i=0;
@Override
public void run() {
try{
while(true){
i++;
System.out.println("i= " + (i));
Thread.sleep(1000);
}
}catch(InterruptedException e){
e.printStackTrace ();
}
}
}
package threadTestSHXC;
public class Run {
public static void main(String[] args){
try{
MyThread thread=new MyThread();
thread.setDaemon(true);
thread.start();
Thread.sleep(5000);
. System OUT .println ( "main thread terminates, the guardian of the thread ends without printed");
}catch(InterruptedException e){
e.printStackTrace ();
}
}
}
result:
i= 1
i= 2
i= 3
i= 4
i= 5
main thread terminates, the guardian of the thread ends without printed