Scenario 1:
A single-process program can be bound with a port. When the program starts, you can try to see if the port is already in use, and if so, the program has started.
Scenario 2:
You can create a hidden file in a Java program and delete the file when the program exits. This way, when the program starts, you determine if the file exists, if the description is already started.
Import Java.io.file;import java.io.ioexception;import Java.io.randomaccessfile;import java.nio.channels.FileLock;/ * * Filelockmanager file Lock Management * */public class Filelockmanager {private Filelock filelock = null; Private file File = null; Private Randomaccessfile randomaccessfile = null; Public Filelockmanager (String filename) {this.file = new file (filename); Public Filelockmanager (file file) {this.file = file; /** * file Lock and create file * * @return * @throws IOException */public Boolean lock () throws IOException {if (!this.file.exists ()) {this.file.createNewFile (); This.randomaccessfile = new Randomaccessfile (this.file, "RW"); This.filelock = This.randomAccessFile.getChannel (). Trylock (); if (This.fileLock.isValid ()) {return true; } else {return false; }} else {return false; }}/** * Unlock and deleteFILE * * @return * @throws IOException */public Boolean unLock () throws IOException {if (!this. File.exists ()) {return true; } else {if (this.filelock! = null) {this.fileLock.release (); } if (this.randomaccessfile! = null) {this.randomAccessFile.close (); } if (This.file.delete ()) {return true; } else {return false; }}}/** * @return Returns the Filelock. */Public Filelock Getfilelock () {return this.filelock; }/** * @param filelock * The Filelock to set. */public void Setfilelock (Filelock filelock) {this.filelock = Filelock; }}
Java programs start only once on a computer, only one process is opened