import java.io.*;
import java.net.*;public class JustOne {
  SimpleDummyServer sds = null;    public static void main(String args[]){
    new JustOne().doit();
    } public void doit() {
    try {
      Socket clientSocket = new Socket("localhost", SimpleDummyServer.port);
      System.out.println("*** Already running!");
      System.out.println("Program Exit!");
      System.exit(1);
      }
    catch (Exception e) {
      e.printStackTrace();
      sds = new SimpleDummyServer();
      sds.start();
      }
    
    while(true) {
      try { System.out.print("runing..."); Thread.sleep(1000); }
      catch(Exception e) { e.printStackTrace(); }
      }
    }
 }================================================================
import java.io.*;
import java.net.*;public class SimpleDummyServer extends Thread {
   // you may need to customize this for your machine
   public static final int port = 80 ;    ServerSocket serverSocket = null;
   Socket clientSocket = null;   public void run() {
    try {
      // Create the server socket
      serverSocket = new ServerSocket(port, 1);
      while (true) {
       // Wait for a connection
       clientSocket = serverSocket.accept();
       System.out.println("*** Got a connection! ");
       clientSocket.close();
       }
      }
    catch (IOException ioe) {
     System.out.println("Error in SimpleDummyServer: " + ioe);
     }
    }
  }

解决方案 »

  1.   

    用socket来判断同一个实例有没有存在,若已经有个socket存在,即已经有个实例了。
      

  2.   

    Hi EyeWolf :)
    不知道这样可不可以呢?
    给class OnlyInstance增加下面这个方法:
    --
    public OnlyInstance crashMe() {
        instance = null;
        instance = new OnlyInstance();
        
        return null;
    }然后, 当你象以往一样创建对象:
    OnlyInstance oi = OnlyInstance.getOnlyInstance();
    在不需要oi的时候可以
    oi = oi.crashMe();
    需要新建的时候可以
    oi = OnlyInstance.getOnlyInstance();这样可以吗? 试试看, 然后告诉我答案, 谢谢!