< prev index next >

src/java.base/windows/classes/java/lang/ProcessImpl.java

Print this page

 30 import java.io.File;
 31 import java.io.FileDescriptor;
 32 import java.io.FileInputStream;
 33 import java.io.FileOutputStream;
 34 import java.io.IOException;
 35 import java.io.InputStream;
 36 import java.io.OutputStream;
 37 import java.lang.ProcessBuilder.Redirect;
 38 import java.security.AccessController;
 39 import java.security.PrivilegedAction;
 40 import java.util.ArrayList;
 41 import java.util.Locale;
 42 import java.util.concurrent.CompletableFuture;
 43 import java.util.concurrent.TimeUnit;
 44 import java.util.regex.Matcher;
 45 import java.util.regex.Pattern;
 46 
 47 import jdk.internal.access.JavaIOFileDescriptorAccess;
 48 import jdk.internal.access.SharedSecrets;
 49 import jdk.internal.ref.CleanerFactory;

 50 import sun.security.action.GetBooleanAction;
 51 import sun.security.action.GetPropertyAction;
 52 
 53 /* This class is for the exclusive use of ProcessBuilder.start() to
 54  * create new processes.
 55  *
 56  * @author Martin Buchholz
 57  * @since   1.5
 58  */
 59 
 60 final class ProcessImpl extends Process {
 61     private static final JavaIOFileDescriptorAccess fdAccess
 62         = SharedSecrets.getJavaIOFileDescriptorAccess();
 63 
 64     // Windows platforms support a forcible kill signal.
 65     static final boolean SUPPORTS_NORMAL_TERMINATION = false;
 66 
 67     /**
 68      * Open a file for writing. If {@code append} is {@code true} then the file
 69      * is opened for atomic append directly and a FileOutputStream constructed

541     public InputStream getInputStream() {
542         return stdout_stream;
543     }
544 
545     public InputStream getErrorStream() {
546         return stderr_stream;
547     }
548 
549     private static final int STILL_ACTIVE = getStillActive();
550     private static native int getStillActive();
551 
552     public int exitValue() {
553         int exitCode = getExitCodeProcess(handle);
554         if (exitCode == STILL_ACTIVE)
555             throw new IllegalThreadStateException("process has not exited");
556         return exitCode;
557     }
558     private static native int getExitCodeProcess(long handle);
559 
560     public int waitFor() throws InterruptedException {
561         waitForInterruptibly(handle);




562         if (Thread.interrupted())
563             throw new InterruptedException();
564         return exitValue();
565     }
566 
567     private static native void waitForInterruptibly(long handle);
568 
569     @Override
570     public boolean waitFor(long timeout, TimeUnit unit)
571         throws InterruptedException
572     {
573         long remainingNanos = unit.toNanos(timeout);    // throw NPE before other conditions
574         if (getExitCodeProcess(handle) != STILL_ACTIVE) return true;
575         if (timeout <= 0) return false;
576 
577         long deadline = System.nanoTime() + remainingNanos;
578         do {
579             // Round up to next millisecond
580             long msTimeout = TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L);
581             if (msTimeout < 0) {
582                 // if wraps around then wait a long while
583                 msTimeout = Integer.MAX_VALUE;
584             }
585             waitForTimeoutInterruptibly(handle, msTimeout);





586             if (Thread.interrupted())
587                 throw new InterruptedException();
588             if (getExitCodeProcess(handle) != STILL_ACTIVE) {
589                 return true;
590             }
591             remainingNanos = deadline - System.nanoTime();
592         } while (remainingNanos > 0);
593 
594         return (getExitCodeProcess(handle) != STILL_ACTIVE);
595     }
596 
597     private static native void waitForTimeoutInterruptibly(
598         long handle, long timeoutMillis);
599 
600     @Override
601     public void destroy() {
602         terminateProcess(handle);
603     }
604 
605     @Override

 30 import java.io.File;
 31 import java.io.FileDescriptor;
 32 import java.io.FileInputStream;
 33 import java.io.FileOutputStream;
 34 import java.io.IOException;
 35 import java.io.InputStream;
 36 import java.io.OutputStream;
 37 import java.lang.ProcessBuilder.Redirect;
 38 import java.security.AccessController;
 39 import java.security.PrivilegedAction;
 40 import java.util.ArrayList;
 41 import java.util.Locale;
 42 import java.util.concurrent.CompletableFuture;
 43 import java.util.concurrent.TimeUnit;
 44 import java.util.regex.Matcher;
 45 import java.util.regex.Pattern;
 46 
 47 import jdk.internal.access.JavaIOFileDescriptorAccess;
 48 import jdk.internal.access.SharedSecrets;
 49 import jdk.internal.ref.CleanerFactory;
 50 import jdk.internal.misc.Blocker;
 51 import sun.security.action.GetBooleanAction;
 52 import sun.security.action.GetPropertyAction;
 53 
 54 /* This class is for the exclusive use of ProcessBuilder.start() to
 55  * create new processes.
 56  *
 57  * @author Martin Buchholz
 58  * @since   1.5
 59  */
 60 
 61 final class ProcessImpl extends Process {
 62     private static final JavaIOFileDescriptorAccess fdAccess
 63         = SharedSecrets.getJavaIOFileDescriptorAccess();
 64 
 65     // Windows platforms support a forcible kill signal.
 66     static final boolean SUPPORTS_NORMAL_TERMINATION = false;
 67 
 68     /**
 69      * Open a file for writing. If {@code append} is {@code true} then the file
 70      * is opened for atomic append directly and a FileOutputStream constructed

542     public InputStream getInputStream() {
543         return stdout_stream;
544     }
545 
546     public InputStream getErrorStream() {
547         return stderr_stream;
548     }
549 
550     private static final int STILL_ACTIVE = getStillActive();
551     private static native int getStillActive();
552 
553     public int exitValue() {
554         int exitCode = getExitCodeProcess(handle);
555         if (exitCode == STILL_ACTIVE)
556             throw new IllegalThreadStateException("process has not exited");
557         return exitCode;
558     }
559     private static native int getExitCodeProcess(long handle);
560 
561     public int waitFor() throws InterruptedException {
562         if (Thread.currentThread().isVirtual()) {
563             Blocker.managedBlock(() -> waitForInterruptibly(handle));
564         } else {
565             waitForInterruptibly(handle);
566         }
567         if (Thread.interrupted())
568             throw new InterruptedException();
569         return exitValue();
570     }
571 
572     private static native void waitForInterruptibly(long handle);
573 
574     @Override
575     public boolean waitFor(long timeout, TimeUnit unit)
576         throws InterruptedException
577     {
578         long remainingNanos = unit.toNanos(timeout);    // throw NPE before other conditions
579         if (getExitCodeProcess(handle) != STILL_ACTIVE) return true;
580         if (timeout <= 0) return false;
581 
582         long deadline = System.nanoTime() + remainingNanos;
583         do {
584             // Round up to next millisecond
585             long msTimeout = TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L);
586             if (msTimeout < 0) {
587                 // if wraps around then wait a long while
588                 msTimeout = Integer.MAX_VALUE;
589             }
590             if (Thread.currentThread().isVirtual()) {
591                 long millis = msTimeout;
592                 Blocker.managedBlock(() -> waitForTimeoutInterruptibly(handle, millis));
593             } else {
594                 waitForTimeoutInterruptibly(handle, msTimeout);
595             }
596             if (Thread.interrupted())
597                 throw new InterruptedException();
598             if (getExitCodeProcess(handle) != STILL_ACTIVE) {
599                 return true;
600             }
601             remainingNanos = deadline - System.nanoTime();
602         } while (remainingNanos > 0);
603 
604         return (getExitCodeProcess(handle) != STILL_ACTIVE);
605     }
606 
607     private static native void waitForTimeoutInterruptibly(
608         long handle, long timeoutMillis);
609 
610     @Override
611     public void destroy() {
612         terminateProcess(handle);
613     }
614 
615     @Override
< prev index next >