8015727: Thread safe print function
authorjlaskey
Fri, 31 May 2013 13:04:55 -0300
changeset 17972 2ed196cf0643
parent 17971 c2c2b295234b
child 17973 d220c8157f25
8015727: Thread safe print function Reviewed-by: sundar Contributed-by: james.laskey@oracle.com
nashorn/src/jdk/nashorn/internal/objects/Global.java
--- 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();