Avoid creating always the parentListener. Create only when needed.
Signed-off-by: Bogdan Drutu <[email protected]>
diff --git a/context/src/main/java/io/grpc/Context.java b/context/src/main/java/io/grpc/Context.java
index 4aee3be..27f72a8 100644
--- a/context/src/main/java/io/grpc/Context.java
+++ b/context/src/main/java/io/grpc/Context.java
@@ -184,7 +184,9 @@
}
private ArrayList<ExecutableListener> listeners;
- private CancellationListener parentListener = new ParentListener();
+ // parentListener is initialized when listeners is initialized, and uninitialized when
+ // listeners is uninitialized.
+ private ParentListener parentListener;
final CancellableContext cancellableAncestor;
final Node<Key<?>, Object> keyValueEntries;
// The number parents between this context and the root context.
@@ -526,6 +528,7 @@
listeners = new ArrayList<>();
listeners.add(executableListener);
if (cancellableAncestor != null) {
+ parentListener = new ParentListener();
cancellableAncestor.addListener(parentListener, DirectExecutor.INSTANCE);
}
} else {
@@ -558,6 +561,7 @@
if (cancellableAncestor != null) {
cancellableAncestor.removeListener(parentListener);
}
+ parentListener = null;
listeners = null;
}
}
@@ -573,10 +577,13 @@
return;
}
ArrayList<ExecutableListener> tmpListeners;
+ ParentListener tmpParentListener;
synchronized (this) {
if (listeners == null) {
return;
}
+ tmpParentListener = parentListener;
+ parentListener = null;
tmpListeners = listeners;
listeners = null;
}
@@ -595,7 +602,7 @@
}
}
if (cancellableAncestor != null) {
- cancellableAncestor.removeListener(parentListener);
+ cancellableAncestor.removeListener(tmpParentListener);
}
}