Daemon thread

For threads, there is the concept of a guarded thread. How to understand it? For example, in A thread, new out of thread B, if A is not a daemon thread, then B can still be executed after A ends. If it is a daemon thread, when A ends, B will no longer execute

 

package thread;

 

public class ChildParentThread {

 

public static void main(String args[]){

 

Thread t = new ParentThread("parent");

t.setDaemon(true);

t.start();

}

}

 

class ParentThread extends Thread{

 

public ParentThread(String name){

  super(name);

}

public void run(){

String temp = Thread.currentThread().getName();

System.out.println(temp+" before   ");

new ChildThread("child").start();

 

System.out.println(temp+ " after   ");

 

}

}

 

class ChildThread extends Thread{

 

public ChildThread(String name){

 super(name);

}

public void run(){

String temp = Thread.currentThread().getName();

for(int i =0;i<100;i++){

System.out.println(temp+","+i);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace ();

}

}

 

}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326579489&siteId=291194637