15,守护线程

在启动前调用

前台线程结束后,后台进程自动结束

t.setDaemon(true);
package songyan;
/*
  守护线程(用户线程):后台线程
t.setDaemon(true);//true 表示守护线程
特点:
前台线程都结束后,后台线程自动结束
*/

class Demo implements Runnable{
    boolean flag=true;
    public synchronized void run()
    {
        while(flag)
        {            
            
            try {
                this.wait();
            } 
            catch (Exception e) 
            {
                System.out.println("jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj");
            }
            System.out.println("run ******888888888888888888888888888888888888*");
        }
    }
    public void changeFlag()
    {
        flag=false;
    }
}
public class test {
    public static  void main(String[] args)
    {
        Demo d= new Demo();
        Thread t= new Thread(d);
        t.setDaemon(true);
        t.start();
        
        int count =0;
        
        while(true)
        {
            count++;
            System.out.println("main***"+count);
            if(count==600)
            {                
                break;
            }
            
        }
    }
}
原文地址:https://www.cnblogs.com/exexex/p/8435095.html