<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1, comments - 0, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    探索java多線程(連載)1 守護線程

    Posted on 2011-03-22 19:25 ikon 閱讀(2084) 評論(0)  編輯  收藏

          在java中有一類線程,專門在后臺提供服務,此類線程無需顯式關閉,當程序結束了,它也就結束了,這就是守護線程 daemon thread。如果還有非守護線程的線程在執行,它就不會結束。      守護線程有何用處呢?讓我們來看個實踐中的例子。

          在我們的系統中經常應用各種配置文件(黑名單,禁用詞匯),當修改配置文件后,一般要重啟服務,系統才能夠加載;當重啟服務的代價比較高的情況下,這種加載方式不能滿足我們的要求,這個時候守護線程該發揮它的作用了,它可以實時加載你的配置文件,無需重啟。(當然,相當重要的配置文件,不推薦實時加載)

    package com.ikon.thread.daemon;

    import java.io.File;
    import java.util.*;

    /**
     * 文件監測
     * 
    @author ikon99999
     * 
     
    */

    public abstract class FileWatchdog extends Thread {

     
      
    static final public long DEFAULT_DELAY = 20*1000
     
      
      
    protected HashMap fileList;
     
      
    protected long delay = DEFAULT_DELAY; 
      
      
    boolean warnedAlready = false;
      
      
    boolean interrupted = false;

      
    public static class Entity
      
    {
            File file;
            
    long lastModify;
            Entity(File file,
    long lastModify)
            
    {
                
    this.file = file;
                
    this.lastModify = lastModify;
            }

      }

      
      
    protected  FileWatchdog() {
          fileList 
    = new HashMap ();
        setDaemon(
    true);
      }


     
      
    public  void setDelay(long delay) {
        
    this.delay = delay;
      }


      
    public void addFile(File file)
      
    {
            fileList.put(file.getAbsolutePath(),
    new Entity(file,file.lastModified()));     
      }

      
      
    public boolean contains(File file)
      
    {
            
    if( fileList.get(file.getAbsolutePath()) != nullreturn true;
            
    else return false;
      }

      
      
    abstract   protected   void doOnChange(File file);

      
    protected  void checkAndConfigure() {
          HashMap map 
    = (HashMap)fileList.clone(); 
          Iterator it 
    = map.values().iterator();
          
          
    while( it.hasNext())
          
    {
              
                Entity entity 
    = (Entity)it.next();
                
                
    boolean fileExists;
                
    try {
                  fileExists 
    = entity.file.exists();
                }
     catch(SecurityException  e) 
                
    {
                  System.err.println (
    "Was not allowed to read check file existance, file:["+ entity.file .getAbsolutePath() +"].");
                  interrupted 
    = true
                  
    return;
                }


                
    if(fileExists) 
                
    {
                    
                  
    long l = entity.file.lastModified(); // this can also throw a SecurityException
                  if(l > entity.lastModify) {           // however, if we reached this point this
                        entity.lastModify = l;              // is very unlikely.
                        newThread(entity.file);
                  }

                }

                
    else 
                
    {
                    System.err.println (
    "["+entity.file .getAbsolutePath()+"] does not exist.");
                }

          }

      }

      
      
    private void newThread(File file)
      
    {
          
    class MyThread extends Thread
          
    {
                File f;
                
    public MyThread(File f)
                
    {
                    
    this.f = f;
                }

                
                
    public void run()
                
    {
                    doOnChange(f);
                }

          }

          
          MyThread mt 
    = new MyThread(file);
          mt.start();
      }


      
    public  void run() 
      
    {    
        
    while(!interrupted) {
          
    try {
            Thread.currentThread().sleep(delay);
          }
     catch(InterruptedException e) {
        
    // no interruption expected
          }

          checkAndConfigure();
        }

      }

    }

        FileWatchdog是個抽象類,本身是線程的子類;在構造函數中設置為守護線程;
        此類用hashmap維護著一個文件和最新修改時間值對,checkAndConfigure()方法用來檢測哪些文件的修改時間更新了,如果發現文件更新了則調用doOnChange方法來完成監測邏輯;doOnChange方法是我們需要實現的;看下面關于一個黑名單服務的監測服務:
          

     1package com.ikon.thread.daemon;
     2
     3import java.io.File;
     4
     5/**
     6 * 黑名單服務
     7 * @author ikon99999
     8 * 2011-3-21
     9 */

    10public class BlacklistService {
    11    private File configFile = new File("c:/blacklist.txt");
    12    
    13    public void init() throws Exception{
    14        loadConfig();
    15        ConfigWatchDog dog = new ConfigWatchDog();
    16        dog.setName("daemon_demo_config_watchdog");//a
    17        dog.addFile(configFile);//b
    18        dog.start();//c
    19    }

    20    
    21    public void loadConfig(){
    22        try{
    23            Thread.sleep(1*1000);//d
    24        
    25            System.out.println("加載黑名單");
    26        }
    catch(InterruptedException ex){
    27            System.out.println("加載配置文件失敗!");
    28        }

    29    }

    30        
    31    public File getConfigFile() {
    32        return configFile;
    33    }

    34
    35    public void setConfigFile(File configFile) {
    36        this.configFile = configFile;
    37    }

    38
    39
    40    private class ConfigWatchDog extends FileWatchdog{
    41        
    42        @Override
    43        protected void doOnChange(File file) {
    44            System.out.println("文件"+file.getName()+"發生改變,重新加載");
    45            loadConfig();
    46        }

    47        
    48    }

    49    
    50    public static void main(String[] args) throws Exception {
    51        BlacklistService service = new BlacklistService();
    52        service.init();
    53        
    54        Thread.sleep(60*60*1000);//e
    55    }

    56}

    57

            ConfigWatchDog內部類實現了doOnChange(File file)方法,當文件被修改后,watchdog調用doOnChange方法完成重新加載操作;
            在blackservice的init方法中初始化watchdog線程;
            d:模擬文件加載耗時
            e:主要是防止主線程退出;

            其實上面的FileWatchdog就是取自log4j;
            

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: v片免费在线观看| 日韩亚洲人成网站| 久久国产精品免费专区| 亚洲中文字幕在线观看| 最新亚洲成av人免费看| 亚洲AV无码久久精品蜜桃| 91人人区免费区人人| 精品日韩99亚洲的在线发布| 日韩在线a视频免费播放| 日韩毛片在线免费观看| 国产aⅴ无码专区亚洲av麻豆| 最近中文字幕大全免费版在线| 久久丫精品国产亚洲av不卡| 四虎永久在线精品免费观看视频| 最新国产成人亚洲精品影院| 日本人护士免费xxxx视频| 国产精品美女免费视频观看| 亚洲AV无码成人专区片在线观看| 久久成人国产精品免费软件| 亚洲精品GV天堂无码男同| 中文字幕人成人乱码亚洲电影| 日韩精品无码一区二区三区免费| 久久久久精品国产亚洲AV无码| 国产精品酒店视频免费看| a毛片在线免费观看| 亚洲香蕉久久一区二区 | 亚洲日韩国产二区无码| 无码国模国产在线观看免费| www在线观看免费视频| 亚洲国产精品日韩在线观看| 俄罗斯极品美女毛片免费播放| 日韩成人免费视频| 亚洲av永久无码精品秋霞电影秋 | 成人午夜亚洲精品无码网站| 亚洲成人免费网站| 边摸边脱吃奶边高潮视频免费| 久久久久亚洲av无码专区导航| 四虎免费久久影院| 国产a视频精品免费观看| 免费的黄色的网站| 亚洲成a人片在线观看中文app|