`
sinokaka
  • 浏览: 324275 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Limit Your App To One Instance

阅读更多

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 代码
  1. private static final int RUN_PORT = 9666;    
  2. //use an obscure port    
  3.   
  4. void main(String[] av)    
  5. {    
  6.         try    
  7.         {    
  8.                 java.net.ServerSocket ss = new java.net.ServerSocket(PORT);    
  9.         }    
  10.       catch (java.net.BindException ex)    
  11.         {    
  12.                 System.out.println("Program already running");    
  13.                 System.exit(1);    
  14.         }    
  15.       catch (java.io.IOException ex)    
  16.         {    
  17.                 ex.printStackTrace();    
  18.                 System.exit(1);    
  19.         }    
  20.         //the rest of your code for the main(...) method    
  21. }   
分享到:
评论

相关推荐

    milter-limit安装指南

    ### milter-limit安装指南 #### 一、简介 milter-limit 是 Sendmail 项目下的一个扩展功能,主要用于限制通过邮件服务器发送邮件的数量,以避免服务器被滥用为垃圾邮件的发送平台。它支持对指定的客户端 IP 地址、...

    itk,错误:use /Zm to specify a higher limit解决办法

    use /Zm to specify a higher limit ``` 此错误表明编译器内部堆栈空间已达到限制,导致编译无法继续。通常这种问题出现在包含大量头文件或者依赖复杂模板元编程的大型项目中。下面将详细介绍如何解决这一问题。 ...

    nginx_limit_access_module.zip

    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 { ...

    Android代码-DcCam

    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网易云音乐实战 uni-app是基于Vue.js的跨平台框架,可以快速开发跨平台应用程序。该框架提供了丰富的组件库和插件,可以满足大多数开发需求。在本文中,我们将使用uni-app框架来开发一个网易云音乐应用程序...

    Oracle分页(limit方式的运用)

    ### Oracle分页(LIMIT方式的运用) 在数据库查询操作中,分页是非常常见的需求之一,尤其是在数据量较大的情况下,为了提高用户体验以及减轻服务器压力,我们需要将数据分成若干个页面进行展示。Oracle数据库作为...

    limit传参的使用方法

    ### 使用LIMIT传参的方法 在MySQL数据库操作过程中,`LIMIT`子句是非常实用的一个功能,尤其是在需要限制查询结果集大小的情况下。本文将详细介绍如何在MySQL中使用`LIMIT`结合参数化查询来实现灵活且安全的数据...

    POJ 1002 487-3279 telephone numbers

    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 ...

    关于sqlite使用limit的一些详细说明

    在SQLite中,`LIMIT`子句是SQL查询语句的一部分,用于限制返回的结果集数量。这个功能对于处理大量数据非常有用,特别是当你只需要查看或处理数据集的一部分时。 `LIMIT`子句的基本语法如下: ```sql SELECT ...

    How to limit request by IP on nginx?

    How to limit request by IP on nginx?

    windows系统下内存压力测试工具testlimit.exe

    `testlimit.exe`是一个专门用于Windows系统下的内存压力测试工具,它可以帮助用户了解系统的内存管理能力,检测系统的稳定性和内存使用极限。这个工具通过模拟高内存占用情况,可以暴露潜在的系统瓶颈和内存泄漏问题...

    RamTool - TestLimit

    RamTool - TestLimit 是一款专为测试计算机内存性能和稳定性的工具。这款软件的主要功能是填满内存并测试系统的极限,以评估系统在高负载下的表现和崩溃阈值。通过这种方式,用户可以了解电脑在处理大量数据时的能力...

    Android代码-Rick App

    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分页场景时为什么会慢

    【使用limit,offset分页场景时为什么会慢】 在SQL查询中,使用`LIMIT`和`OFFSET`进行分页操作在大数据量的情况下可能会变得极其缓慢。这是因为`OFFSET`需要跳过指定数量的行,而数据库必须读取并忽略这些行,这在...

    Mysql_limit.zip_limit

    在MySQL数据库系统中,`LIMIT`子句是一个非常重要的部分,它用于限制SQL查询返回的结果集数量。在处理大量数据时,`LIMIT`是优化查询性能、避免过度消耗资源的有效手段。下面我们将深入探讨`LIMIT`子句的工作原理,...

    libipt_limit.rar_limit

    Shared library add-on to iptables to add limit support.

    nginx限制连接数ngx_http_limit_conn_module模块1

    **语法:** `limit_conn_zone $variable zone=name:size;` **配置段:** `http` 此指令用于定义存储会话状态的内存区域,其中键值为 `$variable` 变量的非空值。`size` 参数表示分配给所有键的共享内存大小。例如...

    ebt_limit.rar_limit

    标题中的"ebt_limit.rar_limit"暗示我们正在讨论与网络过滤和限制相关的技术,特别是与Linux内核的Ebtables(Egress Blocking Tables)工具有关。Ebtables是Linux中用于控制数据包流向的一种工具,类似于iptables,...

    mybatis代码生成limit分页修复

    这篇博客文章《mybatis代码生成limit分页修复》可能探讨了在MBG生成的代码中,如何处理分页查询时遇到的问题,特别是与`LIMIT` SQL语句相关的错误。 首先,我们先理解一下`LIMIT`在MySQL中的作用。`LIMIT`用于限制...

Global site tag (gtag.js) - Google Analytics