RLPark 1.0.0
Reinforcement Learning Framework in Java

Command.java

Go to the documentation of this file.
00001 package rlpark.plugin.rltoys.utils;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.IOException;
00005 import java.io.InputStream;
00006 import java.io.InputStreamReader;
00007 
00008 public class Command {
00009 
00010   class StreamReader extends Thread {
00011     private final BufferedReader reader;
00012 
00013     public StreamReader(InputStream input) {
00014       InputStreamReader inputReader = new InputStreamReader(input);
00015       reader = new BufferedReader(inputReader);
00016     }
00017 
00018     @Override
00019     public void run() {
00020       try {
00021         String line = "";
00022         while (process != null) {
00023           line = reader.readLine();
00024           if (line != null)
00025             System.out.println(label + ": " + line);
00026           else
00027             break;
00028         }
00029       } catch (IOException e) {
00030         e.printStackTrace();
00031       }
00032     }
00033   }
00034 
00035   protected Process process;
00036   protected final String label;
00037   private StreamReader outputThread;
00038   private StreamReader errorThread;
00039   private final String[] commandLine;
00040 
00041   public Command(String label, String... commandLine) {
00042     this.label = label;
00043     this.commandLine = commandLine;
00044   }
00045 
00046   public void start() throws IOException {
00047     process = Runtime.getRuntime().exec(commandLine);
00048     outputThread = new StreamReader(process.getInputStream());
00049     errorThread = new StreamReader(process.getErrorStream());
00050     outputThread.start();
00051     errorThread.start();
00052   }
00053 
00054   public void waitFor() throws InterruptedException {
00055     process.waitFor();
00056     process = null;
00057   }
00058 
00059   public void kill() {
00060     if (process != null) {
00061       process.destroy();
00062       process = null;
00063     }
00064     synchronized (outputThread) {
00065       outputThread.notifyAll();
00066     }
00067     synchronized (errorThread) {
00068       errorThread.notifyAll();
00069     }
00070   }
00071 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark