用戶線程和守護(hù)線程的代碼
/*
?* 守護(hù)線程:是為用戶線程服務(wù)的;jvm停止不用等待守護(hù)線程執(zhí)行完畢
?* 默認(rèn)情況下線程都是屬于用戶線程的,虛擬機(jī)等待所有的用戶線程執(zhí)行完畢才會(huì)停止
?*/
public class DaemonTest {
?? ?public static void main(String[] args) {
?? ??? ?God god=new God();
?? ??? ?You you=new You();
?? ??? ?//將用戶線程調(diào)整為守護(hù)線程
?? ??? ?god.setDaemon(true);//默認(rèn)是false的這里我們改為真
?? ??? ?you.start();
?? ??? ?god.start();
?? ??? ?
?? ?}
}
class You extends Thread{
?? ?@Override
?? ?public void run() {
?? ??? ?for(int i=1;i<10;i++) {
?? ??? ??? ?System.out.println("happy life...");
?? ??? ?}
?? ??? ?System.out.println("oooooo");
?? ?}
}
class God extends Thread{
?? ?@Override
?? ?public void run() {
?? ??? ?for(int i=1;i<100*1000;i++) {
?? ??? ??? ?System.out.println("bless you...");
?? ??? ?}
?? ??? ?
?? ?}
}
標(biāo)簽: