如何在 System.exit(0) 之前向终端打印一行?同步问题

How to print a line to terminal before System.exit(0)? Synchronization issue

目标是在调用 System.exit(0); 之前向终端打印一些消息,例如:

System.out.println("Stopping the server...");
System.exit(0);

但是应用程序在打印消息之前就退出了。如何同步呢?使用 Java JDK8 SE.

退出应用程序前必须flush()流。

System.out.println("Stopping the server...");
System.out.flush();
System.exit(0);