java的runtime.exec创建的线程被挂起的解决办法。

转载 2007年10月13日 21:16:00
 

java class process方法exec调用方法proccess proc.exec(command)。当command的输出内容过大时(OS的I/O buffer容纳不下输出流时),程序会被挂起。解决此内问题的方法将command的输出流proc.getErrorStream(),proc.getInputStream()清空。以下为一示例, linux7.2,jdk1.5.0_06下编译通过。

/*************************************************************************************
*Process exec method: clearn output stream first
*by Benny Zhou
*2/27/2007
**************************************************************************************/
/*
*Runtime.exec method. A few things to keep in mind when using this:
*1. Always read from the streams prior to calling waitFor.
*Otherwise you could end up waiting forever on Windows and other OS platforms whose I/O buffers can¡¯t store enough from
*standard out and standard error to ensure the program has finished. These platforms will pause the execution of whatever
*is running until something reads the buffered content from standard  out and standard error. All platforms suffer from
*this, but some platforms have larger buffers than  others. Needless to say, always read from the streams first.
*2. Always read from standard error first.
*If you read from standard out first and the process only writes to standard error, you¡¯ll hang forever waiting to read.
*IF you read from standard error first, you¡¯ll always be okay on these platforms because the OS seems to shutdown standard
*error.
*/
import java.io.*;
import java.lang.*;  
import java.util.*;

public class ProcessExec
{

        static void drainInBackground(final InputStream is) {
                 new Thread(new Runnable(){
                public void run(){
                        try{
                             while( is.read() >= 0 );
                        } catch(IOException e){
                        }
                }
                }).start();
        }

        public void UnpackupDir(String rootpath,String tarname,String backupType) throws IOException
        {
                String unpackdir=rootpath;
                String strUnpackName = "";
                Runtime r=Runtime.getRuntime();
                Process prog=null;

                strUnpackName="./restoreoptarfile.sh "+unpackdir +" "+tarname+" "+backupType;
                try
                {       System.out.println("========before process.exec()");
                        prog=r.exec(strUnpackName);
                        System.out.println("========after process.exec()");
                        System.out.println("========before process.waitFor()");


                        //==========method 2, clearn output stream first
                        drainInBackground(prog.getErrorStream());
                        drainInBackground(prog.getInputStream());


                        /*//==========method 1. get streams and standart out put first, still need clearn output stream
                        BufferedReader stderr = new BufferedReader(new InputStreamReader(prog.getErrorStream()));
                        String LineErr = stderr.readLine();

                        BufferedReader inpbuildtar = new BufferedReader(new InputStreamReader(prog.getInputStream()));
                        String LINE = inpbuildtar.readLine();
                         while (LineErr !=null)
                        {
                                System.out.println("LineError>>>"+LineErr);
                                LineErr = stderr.readLine();
                        }

                        while (LINE != null)
                        {
                                System.out.println("Line>>>"+LINE);
                                LINE = inpbuildtar.readLine();

                        }
                        */

                        int progEnd = prog.waitFor();
                //      inpbuildtar.close();
                //      stderr.close();

                        System.out.println("========after process.waitFor()");
                        System.out.println("========before Runtime.gc()");
                        r.gc();
                        String unpackfiledir = unpackdir +File.separator+tarname;
                        File unpackfile= new File(unpackfiledir);
                        File dirUnpack = new File(unpackdir +File.separator+backupType+File.separator+"structure"+File.separator+"wbxsite.stc");
                        if(!unpackfile.exists() || !dirUnpack.exists())
                        {
                                System.out.println("ERROR>> Tar file do not unpack  please check the parameter !");
                                System.out.println("ERROR>> Please make sure you have executed the command: <chmod +x *.sh>");
                                System.exit(0);
                        }
                        else
                        {}
                }
                catch(Exception e)
                {
                        System.out.println("ERROR>> Tar file is not unpacked successfully!");
                        System.exit(0);
                }
        }

        public static void main(String args[])
        {
                ProcessExec proexec = new ProcessExec();               
                String str_rootpath="/webex_doc/dfs/migrate/backup";
                String str_tarname="everest@wconfig%T25L%2%2@400006466.tar";
                String str_backupType="everest";
                           
                try
                {
                        proexec.UnpackupDir(str_rootpath,str_tarname,str_backupType);
                }
                catch (Exception e)
                {
                        String err = e.getMessage();
                        System.out.println(err);
                        System.exit(0);
                }
        }

java runtime.exec常见问题

Java中Runtime.exec的一些事 博客分类:  Java基础库   0 预备知识 1 不正确的调用exitValue 2不正确的调用waitFor 3 一种可接受的...
  • mengxingyuanlove
  • mengxingyuanlove
  • 2016-02-21 12:09:08
  • 3384

多线程Runtime.getRuntime().exec常见问题

JAVA调用一个bat批处理程序,调用几十次后会莫名的不再执行,无任何异常抛出。千试万试,LOG精细到逐行,只知道走到Runtime.getRuntime().exec()程序就停止了,但打死不知道为...
  • chengly0129
  • chengly0129
  • 2015-11-16 14:15:01
  • 2413

线程池为什么能维持线程不释放,随时运行各种任务?

线程池 之前一直有这个疑问:我们平时使用线程都是各种new Thread(),然后直接在run()方法里面执行我们要做的各种操作,使用完后需要做什么管理吗?线程池为什么能维持住核心线程不释放,一直...
  • cjh94520
  • cjh94520
  • 2017-04-23 20:42:27
  • 3393

Runtime.getRuntime().exec()执行阻塞问题解决

ava中用Runtime.getRuntime().exec() 调用外部程序, 获取"标准输出流", 老是阻塞. 在网上找了找, 觉得应该是"错误输出流"的问题. 果然, 为"错误输出流"单开一个线...
  • andylao62
  • andylao62
  • 2015-03-23 15:51:21
  • 12975

关于Runtime.getRuntime().exec 进程会一直被挂起,一直在等待的原因。

为了方面广大程序员,我还是决定将改bug写在我的博客上。 在网上查找到的关于Runtime.getRuntime().exec执行命令的方法首先出现的的是这个: try { Process...
  • u013266076
  • u013266076
  • 2015-07-04 14:26:15
  • 2737

java Runtime.getRuntime().exec 调用系统脚本/命令注意事项

使用管道命令执行带密码执行sudo命令,在java中老是不成功今天找到方法解决了顺便整理了一下java Runtime.getRuntime().exec 调用脚本命令的注意事项。 使用管道...
  • Q1059081877Q
  • Q1059081877Q
  • 2015-08-28 13:17:50
  • 7696

java中的Runtime类

前言:Java的类库日益庞大,所包含的类和接口也不计其数。但其中有一些非常重要的类和接口,是Java类库中的核心部分。常见的有String、Object、Class、Collection、ClassL...
  • sinat_19171485
  • sinat_19171485
  • 2015-08-23 22:59:08
  • 5098

Java Process waitFor() 挂死问题

1.JDK文档中明确提到 All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the ...
  • clskkk2222
  • clskkk2222
  • 2011-12-02 10:32:16
  • 5797

java调用外部程序挂起原因

Process p = Runtime.getRuntime().exec("my command ...");   int c = p.waitFor();   if (c != 0)   {   ...
  • fh13760184
  • fh13760184
  • 2009-05-06 10:12:00
  • 4262

java runtime exec 运行挂起的问题

这两天让一个同事去做一个ORACLE数据定时备份的工作,结果她写完代码后发现可以导出备份文件,但是使用RUNTIME的时候没办法结束,也就是说这个进程一直处于挂起的状态,是什么原因呢? 为了解决这个...
  • luo_yifan
  • luo_yifan
  • 2011-11-24 10:51:10
  • 2218
收藏助手
不良信息举报
您举报文章:java的runtime.exec创建的线程被挂起的解决办法。
举报原因:
原因补充:

(最多只允许输入30个字)