[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [onionoo/master] Don't share SimpleDateFormat instance which is not thread-safe.



commit df9abfe9a0776cc0952ce098fbefc8e931fd2654
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Thu Apr 17 20:33:37 2014 +0200

    Don't share SimpleDateFormat instance which is not thread-safe.
    
    When we moved date parsing and formatting to a util class in 14a6e59, we
    didn't think of thread-safety of the SimpleDateFormat class.  Turns out
    it's *not* thread-safe.
    
    The hot fix is to create a new SimpleDateFormat instance for each request.
    
    A better solution might be to share a single instance per date format and
    thread, or to share an instance of a thread-safe SimpleDateFormat
    alternative per date format.  Added these as TODOs.
    
    Fixes #11516.
---
 src/org/torproject/onionoo/DateTimeHelper.java |   20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/src/org/torproject/onionoo/DateTimeHelper.java b/src/org/torproject/onionoo/DateTimeHelper.java
index ff61423..4158894 100644
--- a/src/org/torproject/onionoo/DateTimeHelper.java
+++ b/src/org/torproject/onionoo/DateTimeHelper.java
@@ -5,8 +5,6 @@ package org.torproject.onionoo;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.TimeZone;
 
 public class DateTimeHelper {
@@ -42,17 +40,15 @@ public class DateTimeHelper {
 
   public static final String DATEHOUR_NOSPACE_FORMAT = "yyyy-MM-dd-HH";
 
-  private static Map<String, DateFormat> dateFormats =
-      new HashMap<String, DateFormat>();
-
   private static DateFormat getDateFormat(String format) {
-    DateFormat dateFormat = dateFormats.get(format);
-    if (dateFormat == null) {
-      dateFormat = new SimpleDateFormat(format);
-      dateFormat.setLenient(false);
-      dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
-      dateFormats.put(format, dateFormat);
-    }
+    /* TODO We're creating a new SimpleDateFormat instance here, because
+     * we can't share a single instance per date format between multiple
+     * threads.  A better solution would be to share a single instance per
+     * date format and thread, or to share an instance of a thread-safe
+     * alternative per date format. */
+    DateFormat dateFormat = new SimpleDateFormat(format);
+    dateFormat.setLenient(false);
+    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
     return dateFormat;
   }
 

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits