Make sure only single/one instance running – with File Lock and ShutdownHook
来源:互联网 发布:淘宝卖家怎么开通微淘 编辑:程序博客网 时间:2024/05/01 10:38
http://jimlife.wordpress.com/2008/07/21/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/
应该说,这篇文章讲述的是启动、关闭一个程序的最佳实践或常用方案。
涉及的方面是:
1,当程序已经运行时,使用lock文件,防止有人试图再次启动该程序。
2, 程序关闭时,使用shutdown hook保证一些收尾工作被执行。
代码如下,非常非常的简单~
package test.jimmy.filelock;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.channels.FileChannel;import java.nio.channels.FileLock;public class MyApp { private static File f; private static FileChannel channel; private static FileLock lock; public static void main(String[] args) { try { f = new File("RingOnRequest.lock"); // Check if the lock exist if (f.exists()) { // if exist try to delete it //如果抛出IO异常,说明有其他process占用,则停止运行。 //这里不仅仅是检查lock文件是否存在,而且检查是否被占用。 //这样就避免了由于特殊原因导致lock未被删除,进而导致程序无法再次启动的问题。 f.delete(); } // Try to get the lock //这个用法没见过,学习一下 //只有通过channel才能get一个FileLock channel = new RandomAccessFile(f, "rw").getChannel(); lock = channel.tryLock(); if(lock == null) { // File is lock by other application channel.close(); throw new RuntimeException("Only 1 instance of MyApp can run."); } // Add shutdown hook to release lock when application shutdown // 另外一个用法就是使用shutdownhook保证收尾的工作一定会被执行 ShutdownHook shutdownHook = new ShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownHook); //Your application tasks here.. System.out.println("Running"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } catch(IOException e) { throw new RuntimeException("Could not start process.", e); } } public static void unlockFile() { // release and delete file lock try { if(lock != null) { lock.release(); channel.close(); f.delete(); } } catch(IOException e) { e.printStackTrace(); } } static class ShutdownHook extends Thread { public void run() { unlockFile(); } }}
- Make sure only single/one instance running – with File Lock and ShutdownHook
- only one instance
- No bundle URL present Make sure you’re running a packager server or have included a .jsbundle file
- Make sure the Makefile and distinfo file(/usr/ports/graphics/jpeg/distinfo) are up to date
- react native 错误:Make sure you have an Android emulator running or a device connected and have set up
- My One And Only
- Alias "bootstrap.widgets.TbHeroUnit" is invalid. Make sure it points to an existing PHP file and the
- CMake: include called with wrong number of arguments. include() only takes one file.
- android模拟器failed to make and chown /acct/uid: Read-only file system的原因
- Make sure Drupal file types are recognized as PHP files
- ERROR: Running more than one instance of Hudson
- Problems with the lock file
- Promise.then fired with only one argument?
- up and running with cassandra
- up and running with cassandra
- up and running with cassandra
- Up and Running with C++
- Only one SparkContext may be running in this JVM
- HttpClient 学习整理
- EDK 环境搭建
- TrayIcon编程
- 接下来的一年
- Eee PC X101 Meego上网本台北国际电脑展亮相
- Make sure only single/one instance running – with File Lock and ShutdownHook
- 轻仓避险优选个股
- 外链查询工具大全
- “西藏5100水资源”等三家公司在港启动IPO招股
- 自动鞋套机引发的思考
- SQL SERVER 2005 数据库状态为“可疑”的解决方法
- 英特尔的Ultrabook笔记本计划
- TPTP安装与使用问题
- ARM-Linux开机自启动设置