zoukankan      html  css  js  c++  java
  • Dameon Thread是什么

    daemon  thread 名称为守护线程

    Daemon   thread   在Java里面的定义是守护线程,如果虚拟机中只有Daemon   thread   在运行,则虚拟机退出。  
      虚拟机中可能会同时有很多个线程在运行,只有当所有的非守护线程都结束的时候,虚拟机的进程才会结束,不管在运行的线程是不是   main()   线程。   
       

    class   A   extend  Thread{  
              public   void   run(){  
                      for(;;){  
                              System.out.println("Thread   A   run");  
                      }  
              }  
       
              public   static   void   main(String[]   args){  
                      System.out.println("Thread   main   started!");  
                      try{  
                              (new   Thread(new   A())).start();  
                      }   catch   (Exception   e){  
                      }  
                      System.out.println("Thread   main   ended!");  
              }  
      };  
      会一直跑下去,因为main进程结束了,但   A   进程还没有结束,虚拟机不能退出,  
       
      class   A   implements   runnable{  
              public   void   run(){  
                      for(;;){  
                              System.out.println("Thread   A   run");  
                      }  
              }  
       
              public   static   void   main(String[]   args){  
                      System.out.println("Thread   main   started!");  
                      try{  
                              Thread   a   =   new   Thread(new   A());  
                              a.setDaemon(true);  
                              a.start(true);  
                      }   catch(Exception   e){  
                      }  
                      System.out.println("Thread   main   ended!");  
              }  
      };  
       
      main   线程一退出,虚拟机就退出了,因为剩下在跑的   a   线程是守护线程,虚拟机不管它的死活的,直接退出了。

  • 相关阅读:
    Git 一些常用命令
    Opengl VS2008开发环境
    用 HTML5 给 iPad,iPhone 打造速度超快的应用_HTML5研究小组
    理解CSS3 transform中的Matrix(矩阵) « 张鑫旭鑫空间鑫生活
    Google Dremel 原理 如何能 3 秒分析 1PB 开源中国社区
    Perl:过滤注释
    HTML5 History API实现无刷新跳转 蓝飞技术部落格 —— 关注前沿,追求卓越。
    OPENGL编程指南
    Perl : Quantifier follows nothing in regex; marked by
    .js文件无法运行的方法
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331996.html
Copyright © 2011-2022 走看看