url http://www.devx.com/tips/Tip/13745
How can we make sure that only one instance of our Java application is running on a machine? Java does not provide the means of finding out if another instance of our app is running. And, sometimes, we may really want to limit the user of our app to only one running instance. So, what do we do?
One way of dealing with this problem is to have our app create a file under a certain file name when it runs for the first time. Subsequent launches of our app will check for that file. If it already exists, the app knows another instance is already running and will inform the user, then quit. The first instance of the app, however has to delete the file before it exits.
The drawback of this approach is that out first instance of the app may crash, or may be killed by the user, or for whatever reason, it may cease to exist without having a chance to remove the little file that indicates a running instance.
Another way of dealing with this problem is via the use of java.net.ServerSocket class. The idea is to have an instance of ServerSocket listening over some port for as long as the first running instance of our app runs. Consequent launches of our app will try to set up their own ServerSocket over the same port, and will get a java.net.BindException (because we cannot start two versions of a server on the same port), and we'll know that another instance of the app is already running.
Now, if, for any unintentional reason, our app dies, the resources used by the app die away with it, and another instance can start and set the ServerSocket to prevent more instances from running. The following illustrates our approach:
java 代码
- private static final int RUN_PORT = 9666;
-
-
- void main(String[] av)
- {
- try
- {
- java.net.ServerSocket ss = new java.net.ServerSocket(PORT);
- }
- catch (java.net.BindException ex)
- {
- System.out.println("Program already running");
- System.exit(1);
- }
- catch (java.io.IOException ex)
- {
- ex.printStackTrace();
- System.exit(1);
- }
-
- }
分享到:
相关推荐
Delves into advanced topics including how to control multitasking, security services, running apps on multiple iPlatforms and iDevices, enabling in-app purchases, advanced text layout, and building a ...
34 Is there a limit to the ability of people to carry.docx
limit_access_variable zone=one $limit_access_deny; location / { root html; index index.html index.htm; if ($limit_access_deny) { return 403; } } location /limit_interface { ...
fatal error C1076: compiler ... use /Zm to specify a higher limit Generating Code... fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit Generating Code...
And if you want to add a limit to user , that user should only able to record video up to certain length. Like instagram then this demo is for you. Pros Its handles orientations by its own. For both...
Oracle分页(limit方式的运用)Oracle分页(limit方式的运用)
milter-limit 是Sendmail 的一个 filter,用于限制用户发邮件的数量,以防止邮件服务器 被非法用于发送垃圾邮件,它可以实现对指定的客户端 IP、收件人、发件人进行限制。该 filter 可以不做任何修改即可用于Postfix...
One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only ...
This click.dummer.rickapp.App makes it possible to talk like Rick in Rick and Morty. Support me: APK File with new, different signature or get the Rick App from f-Droid. Android 4.0 If you have a ...
C#限制鼠标活动区域,限制鼠标的活动区域,当然也可以解锁
Shared library add-on to iptables to add limit support.
windows系统下内存压力测试工具testlimit.exe
arbitrary limit to number of sense oscillations tolerated in one sample period (observed to be at least 13 in nvidia ).
在mysql中使用limit传参的方法,一个简单的实例
本文档针对mysql分页之limit慢的问题,使用联合索引在大数据量的情况下优化limit分页的性能
限制程序的最大最小化窗口,适合于程序项目开发,或是学习实践c++ builder
WASTEWATER DISCHARGE LIMIT
高速公路出口匝道推荐限速值确定方法,胡思涛,项乔君,超速行驶与速度差大是出口匝道事故高发的主要原因之一。为了减少出口匝道交通事故,可在出口匝道上游段合理设置限速标志,以降低
主要介绍了使用limit,offset分页场景时为什么会慢,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
p-limit, 运行多个保证并发的&异步函数 限制 运行多个保证并发的&异步函数安装$ npm install p-limit用法const pLimit = require('p-limit');const limit = pLim