
为什么有的类的start方法里会判断是否有读写锁?
发布日期:2022-02-10 11:36:50
浏览次数:24
分类:技术文章
本文共 2298 字,大约阅读时间需要 7 分钟。
如题:我们经常会看到有的类的start方法里面会有getLock()等类似的代码?这是为什么呢?什么地方需要用到这样的lock 在本人的工作中发现,比较常见的一类现象是:需要一直让某类程序长跑,即便出现异常,程序自动终止,那除了处理这些异常(比如 将对应的消息放到ExceptionMessage的Queue里面去,然后重新启动程序去处理之前的任务,当然在程序异常终止的时候,是会丢掉 部分消息。那在这种情况下,我们采用的逻辑是创建一个无限的for循环,在其中每五到十分钟调用一次脚本文件,脚本文件负责启动 线程去处理任务(比如外界传入的消息等)。但是,我们希望同一台机器上不要同时有同一脚本文件的两份实例在运行,那么这个时候 getLock()就派上用场了,它在脚本启动的时候判断是否有lock,如果有则不启动,否则启动。具体代码如下: public void start(){
logger.info("start to transmit message from topic:{}", topicName);
StartUpLock lock = null;
try {
lock = getMsgTransmiterlock(lockFileName);
} catch (RuntimeException ex) {
logger.error("failed to get lock. lockFileName:{}", lockFileName, ex);
return;
}
if (null == lock) {
logger.info("message transmiter has been started. lockFileName:{}", lockFileName);
return;
} }
private StartUpLock getMsgTransmiterlock(String fileName) {
StartUpLock lock = new StartUpLock(new File(lockFileName));
if (lock.lock()) {
return lock;
} else {
return null;
}
} public class StartUpLock { private final File file; private FileLock lock = null; private RandomAccessFile raf = null; private FileChannel fc = null; private static Logger logger = LoggerFactory.getLogger(StartUpLock.class); public StartUpLock(File file) { this.file = file; } public boolean lock(){ if(this.lock != null){ logger.warn("have already gotten lock"); return true; } try { if( !file.exists() ){ File dir = file.getParentFile(); if( !dir.exists() ) dir.mkdirs(); file.createNewFile(); } this.raf = new RandomAccessFile(this.file, "rw"); this.fc = raf.getChannel(); this.lock = fc.tryLock(); if( this.lock == null ){ logger.warn(" file have been loked, cannot get lock"); this.close(); return false; } return true; } catch (FileNotFoundException e) { logger.error("file not found", e); this.close(); return false; } catch (IOException e) { logger.error("file io error", e); this.close(); return false; } } public void close(){ if( this.lock != null ){ try { this.lock.release(); } catch (IOException e) { logger.error("file io error", e); }finally{ this.lock = null; } } if( this.fc != null ){ try { this.fc.close(); } catch (IOException e) { logger.error("file io error", e); }finally{ this.fc = null; } } if( this.raf != null ){ try { this.raf.close(); } catch (IOException e) { logger.error("file io error", e); } finally{ this.raf = null; } } } }
转载地址:https://blog.csdn.net/courage89/article/details/8836163 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2023年05月26日 00时40分33秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
Less-3
2022-09-10
Less-2
2022-09-10
Less-1
2022-09-10
less 运算未生效解决办法
2022-09-10
less 的自动化导入
2022-09-10
less 的使用(linux命令)
2020-07-07 02:45:13
LESS 乱码
2020-07-07 02:44:43
less 中的循环(each 方式)
2020-07-07 02:44:13
less less-loader安装失败问题
2020-07-07 02:43:43
Less css扩展语言
2020-07-07 02:43:13
LESRCNN复现记录
2020-07-07 02:42:42
lerna import报错
2020-07-07 02:42:12
Leopard将发布!Google桌面增加支持
2020-07-07 02:41:42
Lenovo家用台式与一体机预装Win8改装Win7的解决方案
2020-07-07 02:41:11
Lenovo ThinkServer RD450X 无法连接到在BMC WebUI中的Java客户端IKVM
2020-07-07 02:40:41
length,length(),size()
2020-07-07 02:40:11
LeNet网络模型的搭建与训练
2020-07-07 02:39:41
LeNet模型及代码详解
2020-07-07 02:39:11
LeNet小结
2020-07-07 02:38:39
LeNet、AlexNet与VGGNet的学习笔记
2020-07-07 02:38:08