From eb282e9a334f72135f8be8a6ec945ebfc01f2244 Mon Sep 17 00:00:00 2001 From: chegar Date: Mon, 12 Jul 2010 18:16:35 +0100 Subject: [PATCH] 6967684: httpserver using a non thread-safe SimpleDateFormat Reviewed-by: michaelm --- .../sun/net/httpserver/ExchangeImpl.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/share/classes/sun/net/httpserver/ExchangeImpl.java b/src/share/classes/sun/net/httpserver/ExchangeImpl.java index 71fc997f8..26453e697 100644 --- a/src/share/classes/sun/net/httpserver/ExchangeImpl.java +++ b/src/share/classes/sun/net/httpserver/ExchangeImpl.java @@ -52,14 +52,16 @@ class ExchangeImpl { boolean http10 = false; /* for formatting the Date: header */ - static TimeZone tz; - static DateFormat df; - static { - String pattern = "EEE, dd MMM yyyy HH:mm:ss zzz"; - tz = TimeZone.getTimeZone ("GMT"); - df = new SimpleDateFormat (pattern, Locale.US); - df.setTimeZone (tz); - } + private static final String pattern = "EEE, dd MMM yyyy HH:mm:ss zzz"; + private static final TimeZone gmtTZ = TimeZone.getTimeZone("GMT"); + private static final ThreadLocal dateFormat = + new ThreadLocal() { + @Override protected DateFormat initialValue() { + DateFormat df = new SimpleDateFormat(pattern, Locale.US); + df.setTimeZone(gmtTZ); + return df; + } + }; private static final String HEAD = "HEAD"; @@ -206,7 +208,7 @@ class ExchangeImpl { PlaceholderOutputStream o = getPlaceholderResponseBody(); tmpout.write (bytes(statusLine, 0), 0, statusLine.length()); boolean noContentToSend = false; // assume there is content - rspHdrs.set ("Date", df.format (new Date())); + rspHdrs.set ("Date", dateFormat.get().format (new Date())); /* check for response type that is not allowed to send a body */ -- GitLab