public class A{
     private static A singleton;
     private A(){}
     public A PrevInstance(){
         if(singleton == null){
             singleton = new A();
         }
         return singleton;
     }
}

解决方案 »

  1.   

    singleton模式可以产生一个实例,可是如果这个类正在执行中,我又执行这个类,这样就有两个实例了。
      

  2.   

    可以通过设置一个标志位来实现,假设我们只允许生成一个数据库连接池实例,代码如下:public class ConnectionPool {  private static boolean m_bIntanceFlag = false; //保证连接池只允许实例化一次的标志
      private static ConnectionPool m_ConnectionPoolInstance; //唯一实例  //构造函数私有以防止通过构造函数实例化
      private ConnectionPool() {
        //do something...
      }  public static ConnectionPool getInstance() {    //判断是否已经存在实例
        if (!m_bIntanceFlag) {
          m_bIntanceFlag = true;
          m_ConnectionPoolInstance = new ConnectionPool();
        }
        else {
          //存在实例返回NULL
          m_ConnectionPoolInstance = null;
        }
        return m_ConnectionPoolInstance;  }  public void finalize() {
        //析构时标志复位
        m_bIntanceFlag = false;
        m_ConnectionPoolInstance = null;
      }  //测试
      public static void main(String args[]) {    ConnectionPool oConnPool1, oConnPool2;    //构造第一个连接池对象
        oConnPool1 = ConnectionPool.getInstance();
        if (oConnPool1 != null)
          System.out.println("获得第一个连接池对象成功!");    //构造第二个连接池对象
        oConnPool2 = ConnectionPool.getInstance();
        if (oConnPool2 == null)
          System.out.println("获得第二个连接池对象失败!");  }
    }
      

  3.   

    再加上同步标记
    synchronized
      

  4.   

    谢谢各位的热心解答。看来是我的提问方式太含糊了,各位是误解我的意思了。
    singleton模式加上同步标记synchronized可以在运行时时保证只生成一个实例,我想要实现的是:
    例如,Sample.class正在运行的时候,如果我再一次执行java Sample,这样就有两个Sample.class在运行,如何避免这一现象。在VB中,我可以通过app.PrevInstance 来判断,如果app.PrevInstance=True直接退出就是了。Java中如何实现这种功能。http://www.mhdn.net/p/2003-01-12/6430.html 这里有一个通过端口占用来判断的例子,不过在系统中占用端口不太好。
      

  5.   

    用Singleton模式
    public class singletonClass () {
      private static final singletonClass INSTANCE = new singletonClass();
      private singletonClass () {}
      public static getInstance () {
        return INSTANCE;
      }
    }
      

  6.   

    执行两次java Sample会出现两个实例么??
      

  7.   

    1. Define a class contains a flag. 
    2. serialize this Object, store it on your local disk.
    3. main() within your app: read and write this flag. if flag true, exit prog. 
       in order to avid adding synchronize code, use a Vector to wrap the flag.
      

  8.   

    本想用临时文件是否可写来判断,可是项目规定用jdk1.3不支持文件锁定,真是郁闷.
    wobelisk能否稍微详细的解释一下第3点,
    用序列化输出和直接往文件中写TRUE有什么不同吗?
    避免同步代码是什么意思?
    另外如果处理终了修改文件内容失败了又如何处理?谢谢.
      

  9.   

    你的问题是进程间通讯问题.可以JNI调用,C语言中可以采用信号量
      

  10.   

    想到了一个法子,执行PS命令得到系统的进程id,然后在标准输出中根据进程id来判断,没有想到的是进程id是JAVA,而不是我以为的类名,真是烦
      

  11.   

    具体来说,是换一个角度考虑。
    由于正在干的项目是用solaris ,所以我采用直接执行solaris命令来输出进程的方法,然后再得到标准输出,从标准输出中判断是否有两个相同的进程。solaris 的系统命令用"ps -all -f"。由于这个项目规定必须用jdk1.3,所以这是没有办法的办法,如果OS是windows的话可以用win32 api来取得进程名。
    如果可以用jdk1.4的话,还是用锁定文件的方式来判断得好。在这里谢谢各位的解答了。
      

  12.   

    single instance
    You may want to prevent the user from spawning multiple copies of your Java application. Here are two approaches: 
    1:Test for the presence of a busy file. If one exists, abort. If not create one. The test and create can be in the bat file that triggers the app or in the app itself. When the app exists it deletes the busy er file. The main problem with this simple approach is if your app crashes, it won't delete the busy file. You have to manually delete it before you can run the app again. 
    2:Another approach is to have your application open a ServerSocket on a particular port number. The OS will prevent other processes from opening a ServerSocket that uses the same port. If you start the application and are unable to open your ServerSocket (i.e. if you get an "address already in use" exception) assume that the application is already running. In that case, you can use a Socket to connect to the running application and pass it whatever commands you like, or just abort.
      

  13.   

    学C时好像用虚基类能实现singleton,去找找数据结构的书吧,java实现的那种,肯定会有。
      

  14.   

    web_spider(蓦然回首,那人却在、灯火阑珊处。) ( 
     forjie(我爱我家) ( 
    please think it over then give a right answer.ok????
      

  15.   

    当然了,大家可以通过调用系统API,例如在windows下可以通过生成一个互斥量mutex或者,往注册表写个标志
    但这样就牺牲了java跨平台的特性。
    也可以先获取系统平台,然后根据不同的平台做不同的处理,但总感觉不是什么好注意。
    还是建议用我上面推荐的1:用文件2:用端口号