Skip to content

Commit 04bd1ef

Browse files
committed
Address comments
1 parent 7f6b211 commit 04bd1ef

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/ThreadLocalByteStringOutputStream.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
*/
3232
public class ThreadLocalByteStringOutputStream {
3333

34-
private static final ThreadLocal<@Nullable SoftReference<RefHolder>> threadLocalRefHolder =
35-
new ThreadLocal<>();
34+
private static final ThreadLocal<SoftRefHolder> threadLocalSoftRefHolder =
35+
ThreadLocal.withInitial(SoftRefHolder::new);
3636

3737
// Private constructor to prevent instantiations from outside.
3838
private ThreadLocalByteStringOutputStream() {}
@@ -88,6 +88,11 @@ public void close() {
8888
}
8989
}
9090

91+
private static class SoftRefHolder {
92+
93+
private @Nullable SoftReference<RefHolder> softReference;
94+
}
95+
9196
private static class RefHolder {
9297

9398
public ByteStringOutputStream stream = new ByteStringOutputStream();
@@ -106,14 +111,13 @@ public static RefHolder create() {
106111
}
107112

108113
private static RefHolder getRefHolderFromThreadLocal() {
109-
@Nullable SoftReference<RefHolder> refHolderSoftReference = threadLocalRefHolder.get();
110-
@Nullable RefHolder refHolder = null;
111-
if (refHolderSoftReference != null) {
112-
refHolder = refHolderSoftReference.get();
113-
}
114-
if (refHolderSoftReference == null || refHolder == null) {
114+
SoftRefHolder softRefHolder = threadLocalSoftRefHolder.get();
115+
RefHolder refHolder;
116+
if (softRefHolder.softReference != null && softRefHolder.softReference.get() != null) {
117+
refHolder = softRefHolder.softReference.get();
118+
} else {
115119
refHolder = RefHolder.create();
116-
threadLocalRefHolder.set(new SoftReference<>(refHolder));
120+
softRefHolder.softReference = new SoftReference<>(refHolder);
117121
}
118122
return refHolder;
119123
}

0 commit comments

Comments
 (0)