8015727: Thread safe print function
Reviewed-by: sundar
Contributed-by: james.laskey@oracle.com
--- a/nashorn/src/jdk/nashorn/internal/objects/Global.java Fri May 31 17:39:12 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/objects/Global.java Fri May 31 13:04:55 2013 -0300
@@ -1628,20 +1628,21 @@
@SuppressWarnings("resource")
private static Object printImpl(final boolean newLine, final Object... objects) {
final PrintWriter out = Global.getEnv().getOut();
+ final StringBuilder sb = new StringBuilder();
- boolean first = true;
for (final Object object : objects) {
- if (first) {
- first = false;
- } else {
- out.print(' ');
+ if (sb.length() != 0) {
+ sb.append(' ');
}
- out.print(JSType.toString(object));
+ sb.append(JSType.toString(object));
}
+ // Print all at once to ensure thread friendly result.
if (newLine) {
- out.println();
+ out.println(sb.toString());
+ } else {
+ out.print(sb.toString());
}
out.flush();