Java programs start only once on a computer, only one process is opened

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.