2

このクラスは、コマンドを実行するための応答であり、結果を出力します

    public class ExecutorTask implements Runnable{

    @Override
    public void run() {

        Process process = null;
        try {
            process = Runtime.getRuntime().exec("cmd /c dir");
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
             String line="";
             while ((line = reader.readLine()) != null) {
                 System.out.println(line);
             }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            return;
        }
    }
}

2 番目のクラスは、スレッドを使用してシェルを実行するエグゼキュータです。

public final class ShellCommandExecutor{

    public void execute(String command){

        ExecutorTask task = new ExecutorTask();
        Thread executorThread = new Thread(task);
        executorThread.start();

        /*try {
            Thread.sleep(1000);
            executorThread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }

}

問題は、クラス ShellCommandExecutor にコード スニペットを追加する必要がある理由です。

try {
        Thread.sleep(1000);
        executorThread.interrupt();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

次に、印刷結果を確認できますか:

2012-08-21  00:32    <DIR>          .
2012-08-21  00:32    <DIR>          ..
2012-08-21  00:32             1,576 .classpath
2012-08-21  00:26             1,224 .project
2012-08-07  10:58    <DIR>          .settings
2012-08-24  15:19            10,965 pom.xml
2012-08-07  10:57    <DIR>          src
2012-08-21  00:32    <DIR>          target
2012-08-24  10:22                 0 velocity.log

なぜ?

4

2 に答える 2

2

あなたはでスレッドを始めました

 executorThread.start();

他に何もしなければ、それを開始したスレッド(メインスレッド)はexecutorThreadが終了するのを待たずに戻るので、このスレッドがタスクを実行する前にアプリケーションは終了します。

executorThreadが終了するのを待つには、次の呼び出しを行う必要があります。

executorThread.join();

コードの後半。この時点で、タスクが完了したことが保証されます。

現在、メインスレッドで1秒間待機し、この1秒間に他のスレッドがアクションを実行するため、機能します。ただし、executorThreadを実行するのに1秒以上必要な場合は機能しないためsleep()、この場合は使用しないでください。

Thread.joinjavadocを参照してください。

于 2012-08-25T11:13:37.337 に答える
0

まず第一に、Stringを使用していないのに、なぜStringをメソッドのパラメーターとして 使用しているのですか...execute()

私はあなたのプログラムを少し変更して試しました sleep()interrupt()

以下のコードを試してください.....

public final class ShellCommandExecutor{

    public void execute(){

        ExecutorTask task = new ExecutorTask();
        Thread executorThread = new Thread(task);
        executorThread.start();

        /*try {
            Thread.sleep(1000);
            executorThread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }

    public static void main(String[] args){


        new ShellCommandExecutor().execute();
    }

}



class ExecutorTask implements Runnable{

    @Override
    public void run() {

        Process process = null;
        try {
            process = Runtime.getRuntime().exec("cmd /c dir");
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
             String line="";
             while ((line = reader.readLine()) != null) {
                 System.out.println(line);
             }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            return;
        }
    }
}
于 2012-08-25T11:14:09.173 に答える