changeset 52147:c83ba72377fc

8212178: Soft reference reclamation race in com.sun.xml.internal.stream.util.ThreadLocalBufferAllocator Reviewed-by: rkennke, kbarrett, joehw
author shade
date Mon, 15 Oct 2018 22:30:32 +0200
parents 27df68106e57
children b1526df0add2
files src/java.xml/share/classes/com/sun/xml/internal/stream/util/ThreadLocalBufferAllocator.java
diffstat 1 files changed, 14 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.xml/share/classes/com/sun/xml/internal/stream/util/ThreadLocalBufferAllocator.java	Mon Oct 15 15:21:54 2018 -0400
+++ b/src/java.xml/share/classes/com/sun/xml/internal/stream/util/ThreadLocalBufferAllocator.java	Mon Oct 15 22:30:32 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -39,15 +39,19 @@
  * @author Santiago PericasGeertsen
  */
 public class ThreadLocalBufferAllocator {
-   private static ThreadLocal<SoftReference<BufferAllocator>> tlba = new ThreadLocal<>();
+    private static final ThreadLocal<SoftReference<BufferAllocator>> TL = new ThreadLocal<>();
 
-   public static BufferAllocator getBufferAllocator() {
-        SoftReference<BufferAllocator> bAllocatorRef = tlba.get();
-        if (bAllocatorRef == null || bAllocatorRef.get() == null) {
-            bAllocatorRef = new SoftReference<>(new BufferAllocator());
-            tlba.set(bAllocatorRef);
+    public static BufferAllocator getBufferAllocator() {
+        BufferAllocator ba = null;
+        SoftReference<BufferAllocator> sr = TL.get();
+        if (sr != null) {
+            ba = sr.get();
         }
-
-        return bAllocatorRef.get();
-   }
+        if (ba == null) {
+            ba = new BufferAllocator();
+            sr = new SoftReference<>(ba);
+            TL.set(sr);
+        }
+        return ba;
+    }
 }