用wait()和notify() or notifyAll()就可以了1

解决方案 »

  1.   

    再贴
    =============================
    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);
         }
        }
      }
    =================================================
    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(); }
          }
        }
     }
      

  2.   

    在类SimpleDummyServer中设置一个静态变量public static int intInstance=0.线程运行时首先检测该变量,如intInstance<=0则置intInstance为1,线程继续运行;如检测到该变量>0,则表明线程已在运行,终止当前线程则可.注意修改该变量时使用线程同步.实际中你可以做得更好.
    (不知此问题是否已解决,此方案仅供参考)
      

  3.   

    在程序间实现还不与在线程间的一样吗?
    将信号量保存在一个资源文件中即可.
    不过修改信号量时难以保证其准确性.
    细节间题自已搞定吧.
    不管是程序间还是线程间,我的想法只是要建立起它们间的一些公共联系.
    有了它们的这些把柄,才好控制它们.
    Windows就喜欢把柄,瞧WinAPI动不动就要一个"把柄".:)