[java] Fixing double-check lock in a lazy initializer
@@ -99,16 +99,17 @@ public Tracer getTracer() {
LOG.info("Using OpenTelemetry for tracing");
- if (tracer != null) {
- return tracer;
- }
-
+ Tracer localTracer = tracer;
+ if (localTracer == null) {
synchronized (LoggingOptions.class) {
- if (tracer == null) {
- tracer = createTracer();
+ localTracer = tracer;
+ if (localTracer == null) {
+ localTracer = createTracer();
+ tracer = localTracer;
+ }
}
}
- return tracer;
+ return localTracer;
}
private Tracer createTracer() {
