简体   繁体   English

System.out.println / System.err.println不起作用

[英]System.out.println/System.err.println not working

I have the following code: 我有以下代码:

  @Test
  public void testMultipleUpdatesSameTime() {
    final CyclicBarrier gate = new CyclicBarrier(3);

    PrintStream out = null;
    try {
      out = new PrintStream(new FileOutputStream(
          "C:\\pathToFile\\test2_output.txt"));
    } catch (FileNotFoundException e1) {
      System.err.println(e1);
    }
    System.setErr(out);

    new Thread(() -> {
      try {
        gate.await();
      } catch (InterruptedException e) {
        System.err.println(e);
      } catch (BrokenBarrierException e) {
        System.err.println(e);
      }
      System.err.println("FROM1: Starting at: " + System.currentTimeMillis());
      System.err.println("FROM1: Thread with ID: " + Thread.currentThread().getId());
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        System.err.println(e);
      }
      System.err.println("FROM1: Ending at: " + System.currentTimeMillis());
    }).start();

    new Thread(() -> {
      try {
        gate.await();
      } catch (InterruptedException e) {
        System.err.println(e);
      } catch (BrokenBarrierException e) {
        System.err.println(e);
      }
      System.err.println("FROM2: Starting at: " + System.currentTimeMillis());
      System.err.println("FROM2: Thread with ID: " + Thread.currentThread().getId());
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        System.err.println(e);
      }
      System.err.println("FROM2: Ending at: " + System.currentTimeMillis());
    }).start();

    System.err.println("NOTHING YET");

    try {
      Thread.sleep(10000);
      gate.await();
    } catch (InterruptedException e) {
      System.err.println(e);
    } catch (BrokenBarrierException e) {
      System.err.println(e);
    }

    System.err.println("DONE");
  }

The result file is containing only half of the output expected: 结果文件仅包含预期输出的一半:

  • NOTHING YET 还没有
  • DONE DONE
  • FROM1: Starting at: 1521464569722 FROM1:开始于:1521464569722
  • FROM2: Starting at: 1521464569722 FROM2:开始于:1521464569722
  • FROM1: Thread with ID: 11 FROM1:ID为11的线程
  • FROM2: Thread with ID: 12 FROM2:ID为12的线程

Do you have any idea why the file does not contain the "Ending at" nor exceptions? 您是否知道为什么文件不包含“结尾为”或异常?

It's a common occurrence that a file is missing some contents if you don't close the file after writing. 如果您在写入后不关闭文件,则文件丢失某些内容的情况很常见。 There are finalizer() methods in many OutputStream subclasses that do close the stream, but they don't always have a chance to get called as is the case here. 许多OutputStream子类中都有finalizer()方法,它们确实关闭了流,但是,在这种情况下,它们并不总是有被调用的机会。

The main thread releases the gate and quits immediately, the other threads quickly run their course and at that point the VM is closed and you can't trust that things finished correctly. 主线程释放闸门并立即退出,其他线程迅速运行,此时虚拟机已关闭,您不能相信事情能正确完成。

The original code is convoluted and it's hard to "fix" something that's broken from the ground up. 原始代码令人费解,很难“修复”从头开始的问题。 For instance in real code you wouldn't have 2 threads competing to write into the same file. 例如,在真实代码中,您不会有2个线程竞争写入同一文件中。 That just doesn't make sense. 那只是没有道理。

In any case the best "fix" for this would be for the main thread to wait for the other threads to finish (instead of sleeping), and then close the file as below. 无论如何,最好的“解决方案”是让主线程等待其他线程完成(而不是休眠),然后按如下所示关闭文件。

Thread t1 = new Thread(() -> ...
Thread t2 = new Thread(() -> ...
t1.start();
t2.start();

t1.join();
t2.join();
out.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM