Process需要注意
2007-01-24 13:58
302 查看
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process创建的子进程没有自己的终端或控制台
import java.io.*;
import java.util.*;
public class PrivateClass
{
static final String COMMAND = "java PrivateClass slave";
public static void main(String[] args) throws Exception
{
if(args.length == 1 && args[0].equals("slave"))
{
for(int i = 20; i > 0; i--)
{
System.out.println( i +
" bottles of beer on the wall" );
System.out.println(i + " bottles of beer");
System.out.println(
"You take on down, pass it around,");
System.out.println( (i-1) +
" bottles of beer on the wall");
System.out.println();
}
}
else
{
//Process process =new ProcessBuilder("java PrivateClass", "slave").start();
Process process = Runtime.getRuntime().exec(COMMAND);
drainInBackground(process.getInputStream());//这样就可以看到调用程序运行的结果
int exitValue = process.waitFor();
System.out.println("exit value = " + exitValue);
}
}
static void drainInBackground(final InputStream is)
{
new Thread()
{
public void run()
{
/*
用下面这个是一样一样的
Scanner s=new Scanner(is);
while(s.hasNextLine())
System.out.println(s.nextLine());
System.out.println("结束");
*/
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String temp;
while((temp=br.readLine())!=null)
System.out.println(temp);
}
catch(IOException e)
{
}
}
}.start();
}
}
经过实际测试,发现 System.out.println("exit value = " + exitValue);
这一句不是最后才打印出来的,它可能在子线程中间就输出
于是我改了一下子程序,算是小技巧吧
///////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
public class PrivateClass
{
static final String COMMAND = "java PrivateClass slave";
public static void main(String[] args) throws Exception
{
if(args.length == 1 && args[0].equals("slave"))
{
for(int i = 80; i > 0; i--)
{
System.out.println( i +
" bottles of beer on the wall" );
System.out.println(i + " bottles of beer");
System.out.println(
"You take on down, pass it around,");
System.out.println( (i-1) +
" bottles of beer on the wall");
System.out.println();
}
}
else
{
Process process = Runtime.getRuntime().exec(COMMAND);
ThreadInBack tib=new ThreadInBack(process.getInputStream());
tib.start();
tib.join();
int exitValue = process.waitFor();
System.out.println("exit value = " + exitValue);
}
}
}
class ThreadInBack extends Thread
{
InputStream is;
public ThreadInBack(InputStream is)
{
this.is=is;
}
public void run()
{
Scanner s=new Scanner(is);
while(s.hasNextLine())
System.out.println(s.nextLine());
System.out.println("结束");
}
}
Process创建的子进程没有自己的终端或控制台
import java.io.*;
import java.util.*;
public class PrivateClass
{
static final String COMMAND = "java PrivateClass slave";
public static void main(String[] args) throws Exception
{
if(args.length == 1 && args[0].equals("slave"))
{
for(int i = 20; i > 0; i--)
{
System.out.println( i +
" bottles of beer on the wall" );
System.out.println(i + " bottles of beer");
System.out.println(
"You take on down, pass it around,");
System.out.println( (i-1) +
" bottles of beer on the wall");
System.out.println();
}
}
else
{
//Process process =new ProcessBuilder("java PrivateClass", "slave").start();
Process process = Runtime.getRuntime().exec(COMMAND);
drainInBackground(process.getInputStream());//这样就可以看到调用程序运行的结果
int exitValue = process.waitFor();
System.out.println("exit value = " + exitValue);
}
}
static void drainInBackground(final InputStream is)
{
new Thread()
{
public void run()
{
/*
用下面这个是一样一样的
Scanner s=new Scanner(is);
while(s.hasNextLine())
System.out.println(s.nextLine());
System.out.println("结束");
*/
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String temp;
while((temp=br.readLine())!=null)
System.out.println(temp);
}
catch(IOException e)
{
}
}
}.start();
}
}
经过实际测试,发现 System.out.println("exit value = " + exitValue);
这一句不是最后才打印出来的,它可能在子线程中间就输出
于是我改了一下子程序,算是小技巧吧
///////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
public class PrivateClass
{
static final String COMMAND = "java PrivateClass slave";
public static void main(String[] args) throws Exception
{
if(args.length == 1 && args[0].equals("slave"))
{
for(int i = 80; i > 0; i--)
{
System.out.println( i +
" bottles of beer on the wall" );
System.out.println(i + " bottles of beer");
System.out.println(
"You take on down, pass it around,");
System.out.println( (i-1) +
" bottles of beer on the wall");
System.out.println();
}
}
else
{
Process process = Runtime.getRuntime().exec(COMMAND);
ThreadInBack tib=new ThreadInBack(process.getInputStream());
tib.start();
tib.join();
int exitValue = process.waitFor();
System.out.println("exit value = " + exitValue);
}
}
}
class ThreadInBack extends Thread
{
InputStream is;
public ThreadInBack(InputStream is)
{
this.is=is;
}
public void run()
{
Scanner s=new Scanner(is);
while(s.hasNextLine())
System.out.println(s.nextLine());
System.out.println("结束");
}
}
相关文章推荐
- zip,collections.defaultdict lambda,csv.DictReader用法简介,及subprocess.call注意事项(需要使用chmod将待执行的文件变为可执行)
- Qt打开外部程序和文件夹需要注意的细节(注意QProcess的空格问题,以及打开本地文件时,需要QUrl::fromLocalFile才可以)
- oracle设置process需要注意的事情
- oracle设置process需要注意的事情
- Java.lang.Process需要注意的问题
- ACCESS转化成SQL2000需要注意的问题
- ACCESS改为SQL需要注意哪几个地方
- web.xml 需要注意的几点
- seo做链轮需要注意的5个细节
- 手机/移动前端开发需要注意的20个要点
- spring管理事务需要注意的
- JavaScript中需要注意的几个问题
- 自定义类型在使用容器存副本时需要注意重载赋值和拷贝构造函数
- 初学单片机需要注意的几个方面
- 绝对定位的特征以及需要注意的情况6条
- struts.xml配置文件中需要注意的地方
- 理解CMM需要注意以下问题
- struts2+spring+hibernate+json+jquery+flexigrid集成和在集成过程中需要注意的问题
- 使用C++实现JNI接口需要注意的事项
- iOS CoreData应用升级需要注意的地方