例子十二.txt

来自「Java大学实用教程 耿祥义编著 课件 PPT」· 文本 代码 · 共 53 行

TXT
53
字号
class Daemon  implements Runnable
{
    Thread A,B;
    Daemon()
    {
       A=new Thread(this);
       B=new Thread(this);
    }
   public void run()
    {
       if(Thread.currentThread()==A)
         {
            for(int i=0;i<8;i++)
             {
                 System.out.println("i="+i) ;
                 try 
                 {
                   Thread.sleep(1000);    
                 }
                 catch(InterruptedException e)
                 {
                 }
             } 
         }
       else if(Thread.currentThread()==B)
         { 
            while(true)
            {
               System.out.println("线程B是守护线程 "); 
               try 
               {
                   Thread.sleep(1000);    
               }
                   catch(InterruptedException e)
               {
               }
            }
         }
      
    }    
}
class Example
{
    public static void main(String args[ ])
    {
        Daemon  a=new Daemon ();
        a.A.start();
        a.B.setDaemon(true);
        a.B.start();
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?