You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by be...@apache.org on 2013/06/17 14:49:51 UTC

git commit: VYSPER-342: after discussing on ML, now prefer thread local again

Updated Branches:
  refs/heads/master 013be8be8 -> 14e2c6a45


VYSPER-342: after discussing on ML, now prefer thread local again


Project: http://git-wip-us.apache.org/repos/asf/mina-vysper/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-vysper/commit/14e2c6a4
Tree: http://git-wip-us.apache.org/repos/asf/mina-vysper/tree/14e2c6a4
Diff: http://git-wip-us.apache.org/repos/asf/mina-vysper/diff/14e2c6a4

Branch: refs/heads/master
Commit: 14e2c6a45895b42fe0cab9ccea2dee2b42e78861
Parents: 013be8b
Author: Bernd Fondermann <be...@brainlounge.de>
Authored: Mon Jun 17 14:43:08 2013 +0200
Committer: Bernd Fondermann <be...@brainlounge.de>
Committed: Mon Jun 17 14:43:08 2013 +0200

----------------------------------------------------------------------
 .../org/apache/vysper/charset/CharsetUtil.java  | 34 +++++++++++++-------
 1 file changed, 22 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-vysper/blob/14e2c6a4/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
----------------------------------------------------------------------
diff --git a/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java b/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
index 3ef49b3..48e1da8 100644
--- a/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
+++ b/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
@@ -33,24 +33,34 @@ public class CharsetUtil {
 
     private static final Charset UTF8 = Charset.forName("UTF-8");
 
-    private static CharsetDecoder decoderCache = null;
-    private static CharsetEncoder encoderCache = null;
+    private static ThreadLocal<CharsetDecoder> decoderCache = new ThreadLocal<CharsetDecoder>();
+    private static ThreadLocal<CharsetEncoder> encoderCache = new ThreadLocal<CharsetEncoder>();
 
-    public static CharsetDecoder getDecoder() {
-        if (decoderCache != null) return decoderCache;
+    private static Object getReference(ThreadLocal threadLocal) {
+        SoftReference reference = (SoftReference) threadLocal.get();
+        if (reference != null) return reference.get();
+        return null;
+    }
 
-        synchronized (CharsetUtil.class) {
-            decoderCache = UTF8.newDecoder();
-        }
-        return decoderCache;
+    private static void setReference(ThreadLocal threadLocal, Object object) {
+        threadLocal.set(new SoftReference(object));
     }
 
     public static CharsetEncoder getEncoder() {
-        if (encoderCache != null) return encoderCache;
+        CharsetEncoder encoder = (CharsetEncoder) getReference(encoderCache);
+        if (encoder == null) {
+            encoder = UTF8.newEncoder();
+            setReference(encoderCache, encoder);
+        }
+        return encoder;
+    }
 
-        synchronized (CharsetUtil.class) {
-            encoderCache = UTF8.newEncoder();
+    public static CharsetDecoder getDecoder() {
+        CharsetDecoder decoder = (CharsetDecoder) getReference(decoderCache);
+        if (decoder == null) {
+            decoder = UTF8.newDecoder();
+            setReference(decoderCache, decoder);
         }
-        return encoderCache;
+        return decoder;
     }
 }
\ No newline at end of file