Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to implement program flow control?

Status
Not open for further replies.

volcano

Programmer
Aug 29, 2000
136
HK
hello, I have a problem. Hope that you can do me a favour. Thanks

I have written a java application. there are mainly 2 sentences inside it, as shown below:

Runtime.getRuntim().exec("WRITE_SOMETHING_TO_A_FILE.exe");
Runtime.getRuntim().exec("READ_THOSE_THINGS_AND_PROCESS.exe");

The problem is that the first command may need a lot of time to process. But then the second command has begun to run without waiting for the finish of the first command. So may I ask if there is any method to let first command finish and then run the second one?

thanks
 
Hi Volcano,

if you hold on to the reference of the
Code:
Process
object that the
Code:
exec()
function returns you can call the
Code:
waitFor()
function on that process to suspend execution until it finishes - for example:

Code:
import java.io.*;
public class testexec{
	public static void main(String[] args){
		try{
			Process p1 = Runtime.getRuntime().exec("...");
			System.out.println("about to wait");
			p1.waitFor();
			System.out.println("finished waiting");	
		}catch(IOException ioex){
			ioex.printStackTrace();
		}catch(InterruptedException iex){
			iex.printStackTrace();
		}
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top