一。程序开始执行时建一个临时文件,退出时删除,如果文件已经存在,那就说明已有实例在运行
二。程序开始时create ServerSocket, listen on a special port,如果不成功,就可以认为已有实例在运行

解决方案 »

  1.   

    何以给一个类本身一个自己的静态变量,把构造子私有化。
    可以参考Java的RunTime类的做法!
      

  2.   

    一。程序开始执行时建一个临时文件,退出时删除,如果文件已经存在,那就说明已有实例在运行It is a good idea, except for a case that the program crashes, then you can never bright it back, even rebooting machine don't solve the problem. 
    One way to solve the problem is to open a file in exclusive mode. So nobody else can open the file. In this way, if program crashes, the lock is removed.
      

  3.   

    public class a
    {
      private static a aa;
      private a();
      public getClassA()
     {
       if(aa==null)aa = new a();
       return aa;
     }
      ...
    }
      

  4.   

    to:skyyoung(路人甲) 
    由于程序在不同进程运行,你的方法好像不行啊,如果是线程就可以。
    Java不能直接访问内存,好像很不好处理这种问题啊。
      

  5.   

    其实有很多方法可以这样做。
    是建立一个互斥的进程外共享通信机制,可以用服务socket,也可以是一个共享文件