Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 可以用java编写自定义重定向吗_Multithreading_Redirect_Processbuilder_System.out_System.err - Fatal编程技术网

Multithreading 可以用java编写自定义重定向吗

Multithreading 可以用java编写自定义重定向吗,multithreading,redirect,processbuilder,system.out,system.err,Multithreading,Redirect,Processbuilder,System.out,System.err,我正在测试一个应用程序。 我的测试很复杂,我产生了两个线程,启动了两个进程构建器,它们产生了两个java进程 是否可以编写一个类似于inherit的自定义重定向,但在每个out和err消息前都添加一些内容,这样我就可以知道它的来源 示例代码如下: public class test { public static void main(String... args){ Thread t = new Thread(new testHelper());

我正在测试一个应用程序。 我的测试很复杂,我产生了两个线程,启动了两个进程构建器,它们产生了两个java进程

是否可以编写一个类似于inherit的自定义重定向,但在每个out和err消息前都添加一些内容,这样我就可以知道它的来源

示例代码如下:

public class test {


    public static void main(String... args){


        Thread t = new Thread(new testHelper());
        t.start();

        t = new Thread(new testHelper());
        t.start();

    }

}

import java.io.IOException;


    public class testHelper implements Runnable {
        @Override
        public void run() {
            Class klass = testWorker.class;



            System.out.println(klass.getCanonicalName());
            String separator = System.getProperty("file.separator");
            String classpath = System.getProperty("java.class.path");
            String path = System.getProperty("java.home")
                    + separator + "bin" + separator + "java";
            ProcessBuilder processBuilder =
                    new ProcessBuilder(path, "-cp",
                            classpath,
                            klass.getCanonicalName());
            processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
            processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
            Process process = null;
            try {
                process = processBuilder.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Child Process is done");


        }
    }

public class testWorker {

    public static void main(String ... args) throws InterruptedException {


        System.out.println("Doing some stuff");
        Thread.sleep(10000);
        System.out.println("Finished doing some stuff");



    }

}

不,不可能。在java.lang.ProcessBuilder.Redirect的源代码中,构造函数是私有的,并且有这样的说法

/**
 * No public constructors.  Clients must use predefined
 * static {@code Redirect} instances or factory methods.
 */
 private Redirect() {}