zoukankan      html  css  js  c++  java
  • Process.waitFor() 死锁问题了解和解决

    转自  https://blog.csdn.net/qq_27948659/article/details/80895860

     public R downLoadFiles(){
    
            logger.info("执行开始下载命令");
            String cmd = //"hadoop fs -ls /files/ftp/zhihui003/oc_data";
                    "hadoop fs -get /files/ftp/zhihui003/* "+fileDir+"";
            //String cmd = "ls  "+fileDir;
            logger.info("downLoadFiles的cmd命令为 :"+cmd);
            // 定时扫描新文件 扫描后处理新文件 分包
            String[] cmds = {"/bin/sh", "-c", cmd };
            Process process=null;
    
            try {
    
                process=Runtime.getRuntime().exec(cmds);
                //获取进程的标准输入流
                final InputStream is1 = process.getInputStream();
                //获取进城的错误流
                final InputStream is2 = process.getErrorStream();
                //启动两个线程,一个线程负责读标准输出流,另一个负责读标准错误流
                new Thread() {
                    public void run() {
                        BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
                        try {
                            String line1 = null;
                            while ((line1 = br1.readLine()) != null) {
                                if (line1 != null){
                                    logger.info(line1);
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        finally{
                            try {
                                is1.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
    
                new Thread() {
                    public void  run() {
                        BufferedReader br2 = new  BufferedReader(new  InputStreamReader(is2));
                        try {
                            String line2 = null ;
                            while ((line2 = br2.readLine()) !=  null ) {
                                if (line2 != null){
                                    logger.info(line2);
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        finally{
                            try {
                                is2.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
    
                //可能导致进程阻塞,甚至死锁
                int ret = process.waitFor();
                System.out.println("return value:"+ret);
                System.out.println(process.exitValue());
                logger.info("event:{}", "RunExeForWindows",process.exitValue());
                byte[] bytes = new byte[process.getInputStream().available()];
                process.getInputStream().read(bytes);
                System.out.println(new String(bytes));
                logger.info("event:{}", "RunExeForWindows",new String(bytes));
            }catch (Exception ex){
                ex.printStackTrace();
                try{
                    process.getErrorStream().close();
                    process.getInputStream().close();
                    process.getOutputStream().close();
                }
                catch(Exception ee){
                    ee.printStackTrace();
                }
            }
            return R.ok("下载成功");
        }
  • 相关阅读:
    advacing lnux program 互斥信号量[copy]
    线程专有数据(ThreadSpecific Data)
    advacing lnux program 条件变量[copy]
    advacing lnux program Thread Cancelation[copy]
    sql 按序号修改
    pku2941 Homogeneous Squares
    pku3051 Satellite Photographs
    pku1222 EXTENDED LIGHTS OUT
    pku3468 A Simple Problem with Integers
    pku2945 Find the Clones
  • 原文地址:https://www.cnblogs.com/huanglei2010/p/11971381.html
Copyright © 2011-2022 走看看