posts - 1, comments - 0, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

探索java多线程(连载)1 守护线程

Posted on 2011-03-22 19:25 ikon 阅读(2065) 评论(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;
        

只有注册用户登录后才能发表评论。


网站导航: