ProcessBuilder进程未运行 [英] ProcessBuilder process not running

查看:523
本文介绍了ProcessBuilder进程未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ProcessBuilder来说,我是新手,正在使用线程.在当前状态下,我有一个J按钮来启动计划的执行程序服务.计划的执行程序服务用于将流程委派给两个流程构建器之一.该应用程序旨在记录用户对话.在对话过程中,x分钟后,它将创建一个wav并将其委派给可用的过程进行转录.当调用转录类时,问题就开始了.该过程已启动,该应用程序按预期运行.但是,在退出父应用程序之前,转录过程实际上不会做任何事情.只有这样,它才会开始.检查任务管理器,它显示为一个进程,但使用0.0%的CPU和约238MB的内存,直到我退出,然后两个进程跳到30%-40%和500-1000 MB的内存.另外,我正在使用.waitFor(),但是正在使用线程来运行.waitFor()进程,因为从我收集到的信息来看,它导致应用程序挂起.我将如何解决此问题.抱歉,我无法提供更多详细信息,但这是我的新手.预先感谢!

I'm fairly new to ProcessBuilder and working with threads. In it's current state I have a J-Button which starts a scheduled executor service. The scheduled executor service is used to delegate a process to one of two process builders. The application is meant to record a user conversation. During the conversation, after x minutes it creates a wav and delegates it to an available process for transcription. The problem begins when the transcription class is called. The process is started and the application runs as expected. However, the transcription process doesn't actually do anything until I exit the parent application. Only then it will begin. Checking the task manager it shows as a process but uses 0.0% of the CPU and around 238MB of memory until I exit then the two processes jump to 30%-40% and 500-1000 MB of memory. Also, I am using the .waitFor() but am using a thread to run the .waitFor() process as from what I gather it causes the application to hang. How would I go about fixing this. Sorry I am unable to provide more details but I'm new to this. Thanks in advance!

public class TranDelegator {
    Future<?> futureTranOne = null;
    Future<?> futureTranTwo = null;
    ExecutorService transcriberOne = Executors.newFixedThreadPool(1);
    ExecutorService transcriberTwo = Executors.newFixedThreadPool(1);

    final Runnable transcribeChecker = new Runnable() {
        public void run() {
            String currentWav = null;
            File inputFile = new File("C:\\convoLists/unTranscribed.txt");

            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(inputFile));
            } catch (FileNotFoundException e1) {
                System.out.println("reader didn't initialize");
                e1.printStackTrace();
            }
            try {
                currentWav = reader.readLine();
            } catch (IOException e) {
                System.out.println("currentWav string issue");
                e.printStackTrace();
            }
            try {
                reader.close();
            } catch (IOException e) {
                System.out.println("reader couldn't close");
                e.printStackTrace();
            }

            if(currentWav != null){
                if (futureTranOne == null || futureTranOne.isDone()) {
                    futureTranOne = transcriberOne.submit((transcriptorOne));
                }

                else if (futureTranTwo == null || futureTranTwo.isDone()) {                     
                    futureTranTwo = transcriberTwo.submit((transcriptorTwo));               
                }
            }
        }
    };

    final Runnable transcriptorOne = new Runnable() {
        public void run() {
            System.out.println("ONE");
            try {
                String classpath = System.getProperty("java.class.path");
                String path = "C:/Program Files/Java/jre7/bin/java.exe";
                ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp",
                        classpath, Transcriber.class.getName());
                Process process = processBuilder.start();
                try {
                    process.waitFor();
                } catch (InterruptedException e) {
                    System.out.println("process.waitFor call failed");
                    e.printStackTrace();
                }
            } catch (IOException e) {
                System.out.println("Unable to call transcribeConvo");
                e.printStackTrace();
            }

        }
    };
    final Runnable transcriptorTwo = new Runnable() {
        public void run() {
            System.out.println("TWO");
            try {
                String classpath = System.getProperty("java.class.path");
                String path = "C:/Program Files/Java/jre7/bin/java.exe";
                ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp",
                        classpath, Transcriber.class.getName());
                Process process = processBuilder.start();
                try {
                    process.waitFor();
                } catch (InterruptedException e) {
                    System.out.println("process.waitFor call failed");
                    e.printStackTrace();
                }
            } catch (IOException e) {
                System.out.println("Unable to call transcribeConvo");
                e.printStackTrace();
            }
        }
    };

}


public class Transcriber {
    public static void main(String[] args) throws IOException,
            UnsupportedAudioFileException {
        retreiveEmpInfo();
        TextoArray saveConvo = new TextoArray();
        ArrayList<String> entireConvo = new ArrayList();
        URL audioURL;
        String currentWav = wavFinder();
        ConfigReader configuration = new ConfigReader();
        ArrayList<String> serverInfo = configuration
                .readFromDoc("serverconfig");

        while (currentWav != null) {
            audioURL = new URL("file:///" + currentWav);
            URL configURL = Transcriber.class.getResource("config.xml");
            ConfigurationManager cm = new ConfigurationManager(configURL);
            Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
            recognizer.allocate(); // allocate the resource necessary for the
                                    // recognizer
            System.out.println(configURL);

            // configure the audio input for the recognizer
            AudioFileDataSource dataSource = (AudioFileDataSource) cm
                    .lookup("audioFileDataSource");
            dataSource.setAudioFile(audioURL, null);

            // Loop until last utterance in the audio file has been decoded, in
            // which case the recognizer will return null.
            Result result;
            while ((result = recognizer.recognize()) != null) {

                String resultText = result.getBestResultNoFiller();
                // System.out.println(result.toString());
                Collections.addAll(entireConvo, resultText.split(" "));
            }
            new File(currentWav).delete();
            saveConvo.Indexbuilder(serverInfo, entireConvo);
            entireConvo.clear();
            currentWav = wavFinder();
        }
        System.exit(0);
    }

    private static String wavFinder() throws IOException {
        String currentWav = null;
        int x = 1;
        File inputFile = new File("C:\\convoLists/unTranscribed.txt");
        File tempFile = new File("C:\\convoLists/unTranscribedtemp.txt");

        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
        String currentLine = null;
        String newLine = System.getProperty("line.separator");

        while ((currentLine = reader.readLine()) != null) {
            if (x == 1) {
                currentWav = currentLine;
            } else {
                writer.write(currentLine);
                writer.write(newLine);
            }
            x = 2;
        }
        reader.close();
        writer.flush();
        writer.close();
        inputFile.delete();
        // boolean successful =
        tempFile.renameTo(inputFile);
        // System.out.println("Success: " + successful);
        // System.out.println("currentWav = " + currentWav);
        return currentWav;
    }

    private static void retreiveEmpInfo() throws IOException {
        File tempFile = new File("C:\\convoLists/tmp.txt");

        BufferedReader reader = new BufferedReader(new FileReader(tempFile));
        CurrentEmployeeInfo.setName(reader.readLine());
        CurrentEmployeeInfo.setUserEmail(reader.readLine());
        CurrentEmployeeInfo.setManagerEmail(reader.readLine());
        reader.close();
    }
}

推荐答案

(来自评论)

看起来进程挂起是由于输出/错误流已满.您需要消耗这些流;可能通过线程.

Looks like process hang is due to out/error streams becoming full. You need to consume these streams; possibly via a thread.

Java7 提供了另一种重定向输出的方式.

Java7 provides another way to redirect output.

相关: http://alvinalexander.com/java/java-exec- processbuilder-process-3

这篇关于ProcessBuilder进程未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆