< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java

Print this page

        

@@ -247,22 +247,26 @@
         // Stream to copy from.
         private final InputStream input;
         // Stream to copy to.
         private final OutputStream output;
 
+        private final Thread thread;
+
         Piper(final InputStream input, final OutputStream output) {
             this.input = input;
             this.output = output;
+            this.thread = new Thread(this, "$EXEC Piper");
         }
 
         /**
          * start - start the Piper in a new daemon thread
+         * @return this Piper
          */
-        void start() {
-            Thread thread = new Thread(this, "$EXEC Piper");
+        Piper start() {
             thread.setDaemon(true);
             thread.start();
+            return this;
         }
 
         /**
          * run - thread action
          */

@@ -293,10 +297,14 @@
                     // Don't care.
                 }
             }
         }
 
+        public void join() throws InterruptedException {
+            thread.join();
+        }
+
         // Exit thread.
     }
 
     // Process exit statuses.
     static final int EXIT_SUCCESS  =  0;

@@ -619,58 +627,60 @@
 
         // Prepare for string based i/o if no redirection or provided streams.
         ByteArrayOutputStream byteOutputStream = null;
         ByteArrayOutputStream byteErrorStream = null;
 
+        final List<Piper> piperThreads = new ArrayList<>();
+
         // If input is not redirected.
         if (inputIsPipe) {
             // If inputStream other than System.in is provided.
             if (inputStream != null) {
                 // Pipe inputStream to first process output stream.
-                new Piper(inputStream, firstProcess.getOutputStream()).start();
+                piperThreads.add(new Piper(inputStream, firstProcess.getOutputStream()).start());
             } else {
                 // Otherwise assume an input string has been provided.
-                new Piper(new ByteArrayInputStream(inputString.getBytes()), firstProcess.getOutputStream()).start();
+                piperThreads.add(new Piper(new ByteArrayInputStream(inputString.getBytes()), firstProcess.getOutputStream()).start());
             }
         }
 
         // If output is not redirected.
         if (outputIsPipe) {
             // If outputStream other than System.out is provided.
             if (outputStream != null ) {
                 // Pipe outputStream from last process input stream.
-                new Piper(lastProcess.getInputStream(), outputStream).start();
+                piperThreads.add(new Piper(lastProcess.getInputStream(), outputStream).start());
             } else {
                 // Otherwise assume an output string needs to be prepared.
                 byteOutputStream = new ByteArrayOutputStream(BUFFER_SIZE);
-                new Piper(lastProcess.getInputStream(), byteOutputStream).start();
+                piperThreads.add(new Piper(lastProcess.getInputStream(), byteOutputStream).start());
             }
         }
 
         // If error is not redirected.
         if (errorIsPipe) {
             // If errorStream other than System.err is provided.
             if (errorStream != null) {
-                new Piper(lastProcess.getErrorStream(), errorStream).start();
+                piperThreads.add(new Piper(lastProcess.getErrorStream(), errorStream).start());
             } else {
                 // Otherwise assume an error string needs to be prepared.
                 byteErrorStream = new ByteArrayOutputStream(BUFFER_SIZE);
-                new Piper(lastProcess.getErrorStream(), byteErrorStream).start();
+                piperThreads.add(new Piper(lastProcess.getErrorStream(), byteErrorStream).start());
             }
         }
 
         // Pipe commands in between.
         for (int i = 0, n = processes.size() - 1; i < n; i++) {
             final Process prev = processes.get(i);
             final Process next = processes.get(i + 1);
-            new Piper(prev.getInputStream(), next.getOutputStream()).start();
+            piperThreads.add(new Piper(prev.getInputStream(), next.getOutputStream()).start());
         }
 
         // Wind up processes.
         try {
             // Get the user specified timeout.
-            long timeout = envVarLongValue("JJS_TIMEOUT");
+            final long timeout = envVarLongValue("JJS_TIMEOUT");
 
             // If user specified timeout (milliseconds.)
             if (timeout != 0) {
                 // Wait for last process, with timeout.
                 if (lastProcess.waitFor(timeout, TimeUnit.MILLISECONDS)) {

@@ -681,10 +691,14 @@
                  }
             } else {
                 // Wait for last process and get exit code.
                 exitCode = lastProcess.waitFor();
             }
+            // Wait for all piper threads to terminate
+            for (final Piper piper : piperThreads) {
+                piper.join();
+            }
 
             // Accumulate the output and error streams.
             outputString += byteOutputStream != null ? byteOutputStream.toString() : "";
             errorString += byteErrorStream != null ? byteErrorStream.toString() : "";
         } catch (InterruptedException ex) {
< prev index next >