changeset 318:64250b3a2f2a

8015727: Thread safe print function Reviewed-by: sundar Contributed-by: james.laskey@oracle.com
author jlaskey
date Fri, 31 May 2013 13:04:55 -0300
parents b4e6cc05ce09
children 295c91f5fdde
files src/jdk/nashorn/internal/objects/Global.java
diffstat 1 files changed, 8 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/objects/Global.java	Fri May 31 17:39:12 2013 +0530
+++ b/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();