浏览 2687 次
锁定老帖子 主题:探索java多线程(连载)1 守护线程
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2011-03-22
在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()) != null) return 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是个抽象类,本身是线程的子类;在构造函数中设置为守护线程;
package com.ikon.thread.daemon; import java.io.File; /** * 黑名单服务 * @author ikon99999 * 2011-3-21 */ public class BlacklistService { private File configFile = new File("c:/blacklist.txt"); public void init() throws Exception{ loadConfig(); ConfigWatchDog dog = new ConfigWatchDog(); dog.setName("daemon_demo_config_watchdog");//a dog.addFile(configFile);//b dog.start();//c } public void loadConfig(){ try{ Thread.sleep(1*1000);//d System.out.println("加载黑名单"); }catch(InterruptedException ex){ System.out.println("加载配置文件失败!"); } } public File getConfigFile() { return configFile; } public void setConfigFile(File configFile) { this.configFile = configFile; } private class ConfigWatchDog extends FileWatchdog{ @Override protected void doOnChange(File file) { System.out.println("文件"+file.getName()+"发生改变,重新加载"); loadConfig(); } } public static void main(String[] args) throws Exception { BlacklistService service = new BlacklistService(); service.init(); Thread.sleep(60*60*1000);//e } }
ConfigWatchDog内部类实现了doOnChange(File file)方法,当文件被修改后,watchdog调用doOnChange方法完成重新加载操作;
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |