zoukankan      html  css  js  c++  java
  • java监控文件运行状态

    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);
    }

    /**
    * 通过文件锁来判断程序是否正在运行
    * @return 如果正在运行返回true,否则返回false
    * @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("程序正在运行") ;
    }
    if(flag) {
    System.out.println("程序没有运行mmmmmmmmm.");
    rv=true;
    }else {
    System.out.println("程序正在运行xxxxx.");
    }
    // 关闭流
    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";//空文件
    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(3000, unit);
    String returnStr = "";
    try {
    returnStr = f1.get(3000, unit).toString();
    System.out.println("f1:"+returnStr);
    // returnStr = f2.get(3000, 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);
    //关闭线程池
    pool.shutdown();
    // 等待子线程结束,再继续执行下面的代码
    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");
    }

    }

  • 相关阅读:
    C# MVC模式设置404&500
    jQuery实现滚动条滚动到子元素位置(方便定位)
    Oracle数据库的导出导入
    Oracle之clob字段不能union的问题
    div中内容垂直居中的方法小结
    C#实现Oracle数据库插入clob字段类型数据
    C_结构体_笔记
    Practice_17_01_ABC
    Practice_17_01_GID
    【转】 vim显示行号、语法高亮、自动缩进的设置
  • 原文地址:https://www.cnblogs.com/leiyf/p/5604679.html
Copyright © 2011-2022 走看看