Multiple execution of a ProcessBuilder in a Java program

放肆的年华 提交于 2019-12-13 21:38:22

问题


I want to use ProcessBuilder in a Java program which will be used many times in the same program. This code snippet is checked for every record in the database. If only one record satisfies the constraints like start time/end time then I don't have any problem, but if two records satisfies any of these constraints then first record, it executes the commands in the processbuilder and redirects output to logfile. Log but the second record, it doesn't executes the commands in the processbuilder and redirects output to the same logfile. There is a similar question about this problem in this site which has not been answered in the below link. I hope that atleast now anyone will go through this issue and give us a solution. I really don't understand where I am doing it wrong. Anyone who has idea about this concept please reply to this question and let me know where I am doing wrong.

multiple processbuilder in a programme

while(rs1.next())
        {
            instance_id = rs1.getString(1);
            startdate = rs1.getString(2);
            starttime = rs1.getString(3);
            endtime = rs1.getString(4);
            enddate = rs1.getString(5);
            if(presentdate.equals(startdate) || presentdate.equals(enddate))
            {
                if(presenttime.equals(starttime))
                {
                    String[] s1 = new String[]{"cmd", "/c","ec2-start-instances",instance_id,">>","D:\\logfile.log"};
                    ProcessBuilder builder1 = new ProcessBuilder(s1);
                    Process p1 = builder1.start();
                }
                else if(presenttime.equals(endtime))
                {
                    String[] s1 = new String[]{"cmd", "/c","ec2-stop-instances",instance_id,">>","D:\\logfile.log"};
                    ProcessBuilder builder1 = new ProcessBuilder(s1);
                    Process p1 = builder1.start();
                }
            }
        }

回答1:


I suggest that you use

p1.waitFor();

to sync this thread with the subprocess before starting another one, especially when they have a resource (D:\logfile.log) in common.



来源:https://stackoverflow.com/questions/24230003/multiple-execution-of-a-processbuilder-in-a-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!