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);
- }
-
- }
分享到:
相关推荐
### milter-limit安装指南 #### 一、简介 milter-limit 是 Sendmail 项目下的一个扩展功能,主要用于限制通过邮件服务器发送邮件的数量,以避免服务器被滥用为垃圾邮件的发送平台。它支持对指定的客户端 IP 地址、...
use /Zm to specify a higher limit ``` 此错误表明编译器内部堆栈空间已达到限制,导致编译无法继续。通常这种问题出现在包含大量头文件或者依赖复杂模板元编程的大型项目中。下面将详细介绍如何解决这一问题。 ...
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 { ...
### 使用LIMIT传参的方法 在MySQL数据库操作过程中,`LIMIT`子句是非常实用的一个功能,尤其是在需要限制查询结果集大小的情况下。本文将详细介绍如何在MySQL中使用`LIMIT`结合参数化查询来实现灵活且安全的数据...
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...
uni-app网易云音乐实战 uni-app是基于Vue.js的跨平台框架,可以快速开发跨平台应用程序。该框架提供了丰富的组件库和插件,可以满足大多数开发需求。在本文中,我们将使用uni-app框架来开发一个网易云音乐应用程序...
### Oracle分页(LIMIT方式的运用) 在数据库查询操作中,分页是非常常见的需求之一,尤其是在数据量较大的情况下,为了提高用户体验以及减轻服务器压力,我们需要将数据分成若干个页面进行展示。Oracle数据库作为...
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 ...
`testlimit.exe`是一个专门用于Windows系统下的内存压力测试工具,它可以帮助用户了解系统的内存管理能力,检测系统的稳定性和内存使用极限。这个工具通过模拟高内存占用情况,可以暴露潜在的系统瓶颈和内存泄漏问题...
在SQLite中,`LIMIT`子句是SQL查询语句的一部分,用于限制返回的结果集数量。这个功能对于处理大量数据非常有用,特别是当你只需要查看或处理数据集的一部分时。 `LIMIT`子句的基本语法如下: ```sql SELECT ...
How to limit request by IP on nginx?
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 ...
【使用limit,offset分页场景时为什么会慢】 在SQL查询中,使用`LIMIT`和`OFFSET`进行分页操作在大数据量的情况下可能会变得极其缓慢。这是因为`OFFSET`需要跳过指定数量的行,而数据库必须读取并忽略这些行,这在...
在MySQL数据库系统中,`LIMIT`子句是一个非常重要的部分,它用于限制SQL查询返回的结果集数量。在处理大量数据时,`LIMIT`是优化查询性能、避免过度消耗资源的有效手段。下面我们将深入探讨`LIMIT`子句的工作原理,...
Shared library add-on to iptables to add limit support.
**语法:** `limit_conn_zone $variable zone=name:size;` **配置段:** `http` 此指令用于定义存储会话状态的内存区域,其中键值为 `$variable` 变量的非空值。`size` 参数表示分配给所有键的共享内存大小。例如...
标题中的"ebt_limit.rar_limit"暗示我们正在讨论与网络过滤和限制相关的技术,特别是与Linux内核的Ebtables(Egress Blocking Tables)工具有关。Ebtables是Linux中用于控制数据包流向的一种工具,类似于iptables,...
这篇博客文章《mybatis代码生成limit分页修复》可能探讨了在MBG生成的代码中,如何处理分页查询时遇到的问题,特别是与`LIMIT` SQL语句相关的错误。 首先,我们先理解一下`LIMIT`在MySQL中的作用。`LIMIT`用于限制...
arbitrary limit to number of sense oscillations tolerated in one sample period (observed to be at least 13 in nvidia ).