首页 > 代码库 > 守护线程

守护线程

package xiancheng;

public class TestDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
T1 t=new T1();
Daemon d=new Daemon();
t.start();
//将d设置成守护线程
d.setDaemon(true);
d.start();
    }
}

class T1 extends Thread{
    public void run(){
        for(int i=0;i<50;i++){
            System.out.println(i);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

class Daemon extends Thread{
    public void run(){
        while (true){
            System.out.println("播放背景音乐");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }}
            }
    
}

 

守护线程