Process需要注意
来源:互联网 发布:希尔排序算法如何稳定 编辑:程序博客网 时间:2024/06/02 11:38
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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需要注意
- oracle设置process需要注意的事情
- Java.lang.Process需要注意的问题
- 使用java.lang.Process类时需要注意的事情
- hibernate需要注意的。
- 页面需要注意的
- 需要注意的东西
- 一些需要注意的
- *XX++ 需要注意
- 求职者需要注意的
- const需要注意的
- 野指针需要注意
- jms需要注意
- Makefile需要注意的
- 指针参数需要注意
- 【重装系统需要注意什么】
- 安装iis需要注意
- 需要注意的地方
- 跟我一起写 Makefile(九)
- 跟我一起写 Makefile(十)
- 跟我一起写 Makefile(十一)
- 跟我一起写 Makefile(十二)
- cramfs文件系统简介
- Process需要注意
- 探索Access数据库最佳NTFS权限设置(转贴)
- 上来看一眼风景
- 基于ADI ADV202的JPEG2000图像压缩与解压缩系统设计
- WordPress 2.1 ELLA 中文版提供下载
- 我Blog中的文章登上了CSDN的.net频道首页
- 80年代后的亿万富翁
- minicom使用简介
- 一个实现将动态页面转为静态的方案