[tor-commits] [onionoo/master] Use a single DateFormat per thread and format.

karsten at torproject.org karsten at torproject.org
Sun Jun 1 09:56:42 UTC 2014


commit 67100bc1ee3d582404def99fc7eb8cac23debc7c
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Wed May 28 10:08:05 2014 +0200

    Use a single DateFormat per thread and format.
    
    DateFormat is not thread-safe, and creating a new instance every time we
    need one only wastes CPU time.  Make sure that there's a single instance
    per thread and format that the thread can use whenever it wants.
---
 src/org/torproject/onionoo/DateTimeHelper.java |   35 ++++++++++++++++++------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/src/org/torproject/onionoo/DateTimeHelper.java b/src/org/torproject/onionoo/DateTimeHelper.java
index 4158894..41ac70b 100644
--- a/src/org/torproject/onionoo/DateTimeHelper.java
+++ b/src/org/torproject/onionoo/DateTimeHelper.java
@@ -5,6 +5,8 @@ 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 {
@@ -40,16 +42,31 @@ public class DateTimeHelper {
 
   public static final String DATEHOUR_NOSPACE_FORMAT = "yyyy-MM-dd-HH";
 
+  private static ThreadLocal<Map<String, DateFormat>> dateFormats =
+      new ThreadLocal<Map<String, DateFormat>> () {
+    public Map<String, DateFormat> get() {
+      return super.get();
+    }
+    protected Map<String, DateFormat> initialValue() {
+      return new HashMap<String, DateFormat>();
+    }
+    public void remove() {
+      super.remove();
+    }
+    public void set(Map<String, DateFormat> value) {
+      super.set(value);
+    }
+  };
+
   private static DateFormat getDateFormat(String format) {
-    /* 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;
+    Map<String, DateFormat> threadDateFormats = dateFormats.get();
+    if (!threadDateFormats.containsKey(format)) {
+      DateFormat dateFormat = new SimpleDateFormat(format);
+      dateFormat.setLenient(false);
+      dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+      threadDateFormats.put(format, dateFormat);
+    }
+    return threadDateFormats.get(format);
   }
 
   public static String format(long millis, String format) {





More information about the tor-commits mailing list