Package com.rmi.clent;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
Import Java.nio.channels.FileChannel;
Import Java.nio.channels.FileLock;
Import java.util.concurrent.Callable;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.Future;
Import Java.util.concurrent.TimeUnit;
Import java.util.concurrent.TimeoutException;
Public class Runcmd implements callable {
String Filelockpath = "";
Public Runcmd (String Filelockpath) {
This.filelockpath =filelockpath;
}
@Override
Public Object Call () throws Exception {
return isrunning (Filelockpath);
}
/**
* Use file Lock to determine if the program is running
* @return Returns True if it is running, false otherwise
* @throws Exception
*/
Private Boolean isrunning (String filelockpath) throws Exception
{
Boolean rv=false;
try {
Randomaccessfile fis = new Randomaccessfile (Filelockpath, "RW");
FileChannel LOCKFC = Fis.getchannel ();
Boolean flag = true;
Filelock flock = null;
try{
if (LOCKFC!=null) {
Flock = Lockfc.lock ();
Flag = true;
}else{
Flag =false;
}
} catch (Exception e) {
Flag = false;
E.printstacktrace ();
throw new Exception ("program is Running");
}
if (flag) {
SYSTEM.OUT.PRINTLN ("The program is not running mmmmmmmmm.");
Rv=true;
}else {
SYSTEM.OUT.PRINTLN ("program is running xxxxx.");
}
Close the stream
Thread.Sleep (10000);
if (flock!=null) {
Flock.release ();
}
Fis.close ();
} catch (FileNotFoundException E1) {
E1.printstacktrace ();
}
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return RV;
}
public void Excutethread (String strFilePath) {
Strfilepath= "D:\\test\\text.txt";//Empty file
System.out.println ("strFilePath:" +strfilepath);
Executorservice pool= Executors.newfixedthreadpool (2);
Runcmd WF =new Runcmd (strFilePath);
Runcmd wf1 =new runcmd (strFilePath);
Future F1 = Pool.submit (WF);
Future F2 = Pool.submit (WF1);
System.out.println ();
Timeunit unit = timeunit.seconds;
F1.get (unit);
String returnstr = "";
try {
RETURNSTR = F1.get (unit). ToString ();
System.out.println ("F1:" +returnstr);
RETURNSTR = F2.get (unit). ToString ();
System.out.println ("F2:" +returnstr);
} catch (Interruptedexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
} catch (Executionexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
} catch (TimeoutException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
System.out.println (">>>" +returnstr);
Close the thread pool
Pool.shutdown ();
Wait for the child thread to finish before continuing with the following code
try {
Pool.awaittermination (30000, timeunit.seconds);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static void Main (string[] args) {
Runcmd tk = new Runcmd ("D:\\test\\text.txt");
tk. Excutethread ("D:\\test\\text.txt");
}
}
Java Monitor file run status