Run one instance of java application [duplicate]

你说的曾经没有我的故事 提交于 2019-12-08 04:50:45

问题


Possible Duplicate:
How to implement a single instance Java application?

is there a way to run only one instance of Java application so only I have one process? . is it possible to do it in java?


回答1:


A simple way to have one instance is to use a service port.

ServerSocket ss = new ServerSocket(MY_PORT);

The benefit of using this approach instead of a locking a file is that you communicate to the instance already running and even check it is working. e.g. if you can't start the server socket use a plain Socket to send it a message like "open a file for me"




回答2:


The simplest way to do this would be to create a lock file on disk when the application starts if the file does not exist run normally. If the file does exist you can assume another instance of your application is running and exit with a message. Assuming I understood you question correctly.




回答3:


If you mean "having one instance of your application running" then yes, you could use a lock file to acheive that. When your application starts, create a file and remove it when the program exits. At startup, check if the lock file is there. If the file exists then just exit as another instance of your application is already running.




回答4:


You could open a socket on startup. If the socket is in use, there is probably already an instance of the app running. A lock file would work, but if your app crashes without deleting the lock file, you'll have to manually delete the file before you can start the app again.




回答5:


You can apply Singleton Pattern

Following are the ways to do it:

1. Private Constructor and Synchronized method

public class MyClass{

   private static MyClass unique_instance;

   private MyClass(){

         // Initialize the state of the object here
    }

   public static synchronized MyClass getInstance(){

          if (unique_instance == null){

                 unique_instance = new MyClass();

           }

          return unique_instance;

      }

    }

2. Private Constructor and initializing the static instance during declaration

public class MyClass{

   private static MyClass unique_instance = new MyClass() ;

   private MyClass(){

         // Initialize the state of the object here
    }

   public static MyClass getInstance(){


          return unique_instance;

      }

    }

3. Double check Locking

public class MyClass{

   private static MyClass unique_instance;

   private MyClass(){

         // Initialize the state of the object here
    }

   public static MyClass getInstance(){

          if (unique_instance == null)

                      synchronized(this){

          if (unique_instance == null){

                 unique_instance = new MyClass();
               }
           }

          return unique_instance;

      }

    }

You can also implement a class with static methods and static variables to apply Singleton pattern, but its not recommended.



来源:https://stackoverflow.com/questions/11179849/run-one-instance-of-java-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!