Change to ThreadLocal to prevent potential sync bottleneck on cookie creation
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 13 Feb 2009 20:46:22 +0000 (20:46 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 13 Feb 2009 20:46:22 +0000 (20:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@744238 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/tomcat/util/http/ServerCookie.java

index b166220..8b1d258 100644 (file)
@@ -57,14 +57,20 @@ public class ServerCookie implements Serializable {
     // Other fields
     private static final String OLD_COOKIE_PATTERN =
         "EEE, dd-MMM-yyyy HH:mm:ss z";
-    private static final DateFormat OLD_COOKIE_FORMAT;
+    private static final ThreadLocal<DateFormat> OLD_COOKIE_FORMAT =
+        new ThreadLocal<DateFormat>() {
+        protected DateFormat initialValue() {
+            DateFormat df =
+                new SimpleDateFormat(OLD_COOKIE_PATTERN, Locale.US);
+            df.setTimeZone(TimeZone.getTimeZone("GMT"));
+            return df;
+        }
+    };
     private static final String ancientDate;
 
 
     static {
-        OLD_COOKIE_FORMAT = new SimpleDateFormat(OLD_COOKIE_PATTERN, Locale.US);
-        OLD_COOKIE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
-        ancientDate = OLD_COOKIE_FORMAT.format(new Date(10000));
+        ancientDate = OLD_COOKIE_FORMAT.get().format(new Date(10000));
     }
 
     /**
@@ -300,12 +306,10 @@ public class ServerCookie implements Serializable {
                 if (maxAge == 0)
                     buf.append( ancientDate );
                 else
-                    synchronized (OLD_COOKIE_FORMAT) {
-                        OLD_COOKIE_FORMAT.format(
-                                new Date(System.currentTimeMillis() +
-                                        maxAge*1000L),
-                                buf, new FieldPosition(0));
-                    }
+                    OLD_COOKIE_FORMAT.get().format(
+                            new Date(System.currentTimeMillis() +
+                                    maxAge*1000L),
+                            buf, new FieldPosition(0));
             }
         }