Try with trylock to ensure uniqueness of instance

xiaoxiao2021-03-06  50

In order to make your own Application won't start multiple instance, I have made the following try:

Private static stringbuffer tmpfile = new stringbuffer ();

Private static boolean checkinstance () {

Boolean RST = FALSE;

TMPFILE.APpend (System.GetProperty ("java.io.tmpdir");

TMPFILE.APpend ("HodEPRN.TMP");

File fp = new file (tmpfile.toString ());

IF (fp.exiss ()) {

Try {

FileOutputStream Fos = New fileoutputStream (fp.getpath ());

IF (fos.getchannel (). trylock () == null) {

RST = false;

}

Else

RST = true;

} catch (ioexception e) {

RST = false;

}

}

Else {

Try {

fp.createNewFile ();

FileOutputStream Fos = New fileoutputStream (fp.getpath ());

IF (fos.getchannel (). trylock () == null) {

RST = false;

}

Else

RST = true;

} catch (ioexception e) {

RST = false;

}

}

Return RST;

}

It turned out that JNi was used to implement, but always used JNI to do this real material, this barely, tested it on the Windows platform, and deleted a Temporary file from Lock with SYSTEM privilege.

The next is a new Java, and I haven't gotten out, I also hope that you are correct.

转载请注明原文地址:https://www.9cbs.com/read-80407.html

New Post(0)