【问题标题】:How can i stop my process until the previous step gets over我怎样才能停止我的过程,直到上一步结束
【发布时间】:2017-03-01 07:28:53
【问题描述】:

我在 java 中编写了一个脚本,使用它执行 .exe 文件来安装应用程序,之后我将通过安装生成的文件复制到远程位置。但问题是在安装完成之前,将文件复制到机器位置的下一步正在发生。因此导致粘贴空文件,因为安装没有完成,所以文件在那里。我怎样才能停止我的过程,直到上一步结束。我尝试使用 thread.sleep(10000) 但这无济于事,因为安装时间可能会有所不同。

public class threadJoin {
public static void main(String s[]){
    Runnable r = new Runnable() {
        @Override
        public void run() {
            File f = new File("C:\\D\\EVProject\\");

            FilenameFilter textFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
            return name.startsWith("EVProject");
    }
};

File[] files = f.listFiles(textFilter);

for (File file : files) {     

                try {
                    String filexy = file.getCanonicalPath();
                    System.out.print(filexy);
                    Runtime.getRuntime().exec(filexy);
                    //Thread.sleep(10000);
                } catch (IOException ex) {
                    Logger.getLogger(threadJoin.class.getName()).log(Level.SEVERE, null, ex);
                }
        }


        }
    };
    Runnable r2 = new Runnable() {
        @Override
        public void run() {
            String l ="C:\\Program Files\\PD ";
            String m = "C:\\0.0.9.8";
            File srcDir = new File(l);
            File destDir = new File(m);
    try {           
        FileUtils.copyDirectory(srcDir, destDir);
    } catch (IOException e) {
    }
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    };


    Thread t1= new Thread(r);
    Thread t2 = new Thread(r2);
    t1.start();
    try{
        t1.join();
    }catch(InterruptedException e){          
    }
    t2.start();

}

}

【问题讨论】:

  • 你能显示代码吗?
  • 你的程序怎么知道安装完成了?
  • 它不检查安装是否完成。我的程序只是调用 .exe 文件@DanielWiddis

标签: java multithreading join installation


【解决方案1】:

Runtime.exec 返回一个进程句柄。您可以使用此进程句柄来等待进程结束。句柄还可以为您提供一个 int 值,表明该过程是否成功,但这需要您进行测试,因为它可能因实现而异。

在你的情况下 - 试试下面

进程 myP = Runtime.getRuntime().exec(filexy);
int isTrue = myP.waitFor();

看看有没有帮助。

【讨论】:

  • 谢谢它的工作:)。还有一个问题,如果我将文件从一个位置复制粘贴到另一个位置,那么在这种情况下,程序也不应该移动到下一步,直到粘贴完成。请回答:)
  • 如果您使用 Java API 来移动文件,这应该不是问题。
  • 我正在使用 apache-common-io 来复制文件
【解决方案2】:

也许 yuu 可以使用 join 方法 join() ,像这样:

        Runnable task1 = () -> {
            for (int i = 0 ; i < 5 ; i++) {
                System.out.println("task1..." + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}
            }
        };

        Runnable task2 = () -> {
            for (int i = 0 ; i < 5 ; i++) {
                System.out.println("task2..." + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}
            }
        };

        Thread th1 = new Thread(task1);
        Thread th2 = new Thread(task2);

        th1.start();
        try {
            th1.join();
        } catch (InterruptedException e) {}

        th2.start();

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 2020-10-26
    • 2018-11-25
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 2011-09-06
    • 2020-06-20
    相关资源
    最近更新 更多