Java Monitor file run status

Source: Internet
Author: User

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

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.