14

我正在编写一个多线程 Java 程序,其中每个线程都可能需要将其标准输出重定向到一个单独的文件。每个线程都有自己的文件。是否可以在“每个线程”的基础上重定向 System.out,或者是否可以在所有线程中对 System.out 进行全局更改?

4

5 回答 5

26

是否可以在“每个线程”的基础上重定向 System.out

不,这是不可能的。 System.out是静态的,当 JVM 最初启动时,每个 JVM 都有一个作为系统类加载器的一部分加载。虽然当然建议使用每个线程正确的日志记录调用,但我认为您不能这样做是有原因的。System.out以这种方式使用的可能是第 3 方库或其他代码。

您可以做的一件事(作为一个激进的建议)是自己制作PrintStream一个委托给ThreadLocal<PrintStream>. 但是您需要@Override应用程序调用的适当方法才能使其在每个线程中工作。

最后,如果您因为担心并发而问这个问题,System.out那么PrintStream它已经synchronized被隐藏起来并且可以被多个线程安全地使用。

于 2012-04-04T16:18:08.703 回答
8

是否可以在“每个线程”的基础上重定向 System.out

Maia 公司的一些开发人员提供了 PrintStream 的公共实现,该实现为本文中的每个线程提供一个“ STDOUT ”:“线程特定 System.out ”。

在他们的实现中,它们仅覆盖写入方法、flush、close 和 checkError。在他们的情况下似乎就足够了。

正如@Gray在他的回答中所说,他们不需要需要@Override 所有调用的方法以使其在每个线程中工作”。


注释:

请在下面找到来自 Maia 的原始代码。

我在回程机器上找到了它。原始页面已从 Maia 的网站上删除。为了读者的好奇心,我在这里复制它。我不为此代码提供任何支持。


主.java

创建一个 ThreadPrintStream,将其安装为 System.out,并创建并启动 10 个线程。

public class Main {
  public static void main(String[] args) {
    // Call replaceSystemOut which replaces the
    // normal System.out with a ThreadPrintStream. 
    ThreadPrintStream.replaceSystemOut();

    // Create and start 10 different threads.  Each thread
    // will create its own PrintStream and install it into
    // the ThreadPrintStream and then write three messages
    // to System.out.
    for (int i = 0;  i < 10;  i++) {
      Thread thread = new Thread(new StreamText());
      thread.start();

      // Report to the console that a new thread was started.
      System.out.println("Created and started " + thread.getName());
    }
  }
}

StreamText.java

每个线程都有一个简单的 Runnable,它为线程的输出打开一个文件并将其安装到 ThreadPrintStream 中。

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;

/** A small test class that sets System.out for the currently executing
 * thread to a text file and writes three messages to System.out. */
public class StreamText implements Runnable {
  @Override
  public void run() {
    try {
      // Create a text file where System.out.println()
      // will send its data for this thread.
      String name = Thread.currentThread().getName();
      FileOutputStream fos = new FileOutputStream(name + ".txt");

      // Create a PrintStream that will write to the new file.
      PrintStream stream = new PrintStream(new BufferedOutputStream(fos));

      // Install the PrintStream to be used as System.out for this thread.
      ((ThreadPrintStream)System.out).setThreadOut(stream);

      // Output three messages to System.out.
      System.out.println(name + ": first message");
      System.out.println("This is the second message from " + name);
      System.out.println(name + ": 3rd message");

      // Close System.out for this thread which will
      // flush and close this thread's text file.
      System.out.close();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

线程打印流.java

扩展 java.io.PrintStream。ThreadPrintStream 对象替换了普通的 System.out 并为每个线程维护了一个单独的 java.io.PrintStream。

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

/** A ThreadPrintStream replaces the normal System.out and ensures
 * that output to System.out goes to a different PrintStream for
 * each thread.  It does this by using ThreadLocal to maintain a
 * PrintStream for each thread. */
public class ThreadPrintStream extends PrintStream {

  /** Changes System.out to a ThreadPrintStream which will
   * send output to a separate file for each thread. */
  public static void replaceSystemOut() {

    // Save the existing System.out
    PrintStream console = System.out;

    // Create a ThreadPrintStream and install it as System.out
    ThreadPrintStream threadOut = new ThreadPrintStream();
    System.setOut(threadOut);

    // Use the original System.out as the current thread's System.out
    threadOut.setThreadOut(console);
  }

  /** Thread specific storage to hold a PrintStream for each thread */
  private ThreadLocal<PrintStream> out;

  private ThreadPrintStream() {
    super(new ByteArrayOutputStream(0));
    out = new ThreadLocal<PrintStream>();
  }

  /** Sets the PrintStream for the currently executing thread. */
  public void setThreadOut(PrintStream out) {
    this.out.set(out);
  }

  /** Returns the PrintStream for the currently executing thread. */
  public PrintStream getThreadOut() {
    return this.out.get();
  }

  @Override public boolean checkError() {
    return getThreadOut().checkError();
  }

  @Override public void write(byte[] buf, int off, int len) {
    getThreadOut().write(buf, off, len);
  }

  @Override public void write(int b) { getThreadOut().write(b); }

  @Override public void flush() { getThreadOut().flush(); }
  @Override public void close() { getThreadOut().close(); }
}
于 2013-03-20T14:29:53.160 回答
4

你是对的,但不是你想的那样。当一个线程使用

System.out.println();

它需要引用 System.out的副本,而不是 this 引用的对象的副本。

这意味着所有线程通常会看到相同的对象来写入输出。

注意:此字段不是线程安全的,如果您调用此字段,则System.setOut(PrintStream)存在潜在的、不受欢迎的竞争条件,其中不同的线程具有 System.out 的不同本地副本。这不能用来解决这个问题。

是否可以在“每个线程”的基础上重定向 System.out

您可以通过用您自己的线程特定实现替换 System.out 来做到这一点。即 PrintStream 的子类。我这样做是为了记录我希望每个线程的输出保持一致而不是交错的地方。例如,想象一下同时在两个线程中打印两个堆栈跟踪。;)

于 2012-04-04T16:28:42.257 回答
3

System.out是静态的,因此所有线程之间共享同一个实例。

于 2012-04-04T16:18:44.773 回答
0

是否可以在“每个线程”的基础上重定向 System.out

您可以将它们全部重定向到您的委托,该委托将负责“每线程”逻辑。

这是具有自己的文件输出的并行 JBehave 测试的示例。

于 2016-06-07T14:22:05.567 回答