java守護線程讀取設定檔

來源:互聯網
上載者:User
java守護線程讀取設定檔
對於某些應用需要即時讀取設定檔,但是讀取的頻率非常高,比如1秒100次以上,修改設定檔的頻率遠遠低於讀取頻率。此時如果當程式執行的時候每次都
讀取設定檔,那麼系統的io可能會有些壓力,因此可以做一個守護線程每隔一段時間(例如1分鐘)讀取一次設定檔,代碼直接取最新的設定檔的值來執
行,這樣既降低了讀取設定檔的頻率又獲得了較好的即時性。
代碼如下:java 代碼 
  1. import
     java.io.InputStream;  
  2. import
     java.util.Properties;  
  3.   
  4. /**
     
  5.  * 系統設定檔類
     
  6.  * @author sunbin
     
  7.  *
     
  8.  */
      
  9. public
     
    class
     Configation 
    extends
     Thread {  
  10.       
  11.     //靜態屬性類
      
  12.     private
     
    static
     Properties p;  
  13.       
  14.     /**
     
  15.      * 預設構造方法
     
  16.      */
      
  17.     public
     Configation(){  
  18.         //
      
  19.     }  
  20.       
  21.     /**
     
  22.      * 繼承Thread必須要實現的方法
     
  23.      */
      
  24.     public
     
    void
     run(){  
  25.         while
    (
    true
    ){  
  26.             //擷取classpath中設定檔
      
  27.             InputStream in = Configation.class
    .getClassLoader().getResourceAsStream(
    "config.properties"
    );  
  28.             if
     (p == 
    null
    ){  
  29.                 p = new
     Properties();  
  30.             }  
  31.             try
    {  
  32.                 p.load(in); 
  33.                 in.close();
  34.                 Thread.sleep(10000
    );
    //休眠10秒後重新讀取設定檔
      
  35.             }catch
    (Exception e){  
  36.                 e.printStackTrace();  
  37.             }  
  38.         }  
  39.     }  
  40.       
  41.     /**
     
  42.      * 擷取設定檔的執行個體
     
  43.      * @return
     
  44.      */
      
  45.     public
     Properties getProperties(){  
  46.         return
     p;  
  47.     }  
  48.       
  49.     /**
     
  50.      * 測試主程式
     
  51.      * @param args
     
  52.      */
      
  53.     public
     
    static
     
    void
     main(String[] args){  
  54.         Configation c = new
     Configation();  
  55.         c.setDaemon(true
    );
    //設定線程為守護線程
      
  56.         c.start();//啟動線程
      
  57.         try
     {  
  58.             Thread.sleep(3000
    );  
  59.         } catch
     (InterruptedException e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.         //重複列印設定檔的值,當修改設定檔後1秒立即生效
      
  63.         while
    (
    true
    ){  
  64.             Properties p = c.getProperties();  
  65.             System.out.println(p.getProperty("com.test.a"
    ));  
  66.         }  
  67.           
  68.     }  
  69.   
  70. }  

其中c.setDaemon(true)設定線程為守護線程,關於守護線程可以參考http://www.google.cn/search?complete=1&hl=zh-CN&q=java+%E5%AE%88%E6%8A%A4%E7%BA%BF%E7%A8%8B&meta=&aq=t&oq=java+%E5%AE%88%E6%8A%A4

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.