Skip to content

Commit 6aaf18b

Browse files
committed
update for atomicity
1 parent 4a35251 commit 6aaf18b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/ExperimentalSnippetHolder.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55

66
package io.opentelemetry.javaagent.bootstrap.servlet;
77

8+
import java.util.concurrent.atomic.AtomicReference;
9+
810
public class ExperimentalSnippetHolder {
911

10-
private static volatile String snippet = "";
12+
private static final AtomicReference<String> snippet = new AtomicReference<>("");
1113

1214
private static boolean isSet = false;
1315

14-
public static void setSnippet(String snippet) {
15-
if (isSet) {
16-
return;
16+
public static void setSnippet(String newValue) {
17+
String oldValue = snippet.get();
18+
while (!isSet) {
19+
isSet = snippet.compareAndSet(oldValue, newValue);
20+
oldValue = snippet.get();
1721
}
18-
ExperimentalSnippetHolder.snippet = snippet;
19-
isSet = true;
2022
}
2123

2224
public static String getSnippet() {
23-
return snippet;
25+
return snippet.get();
2426
}
2527

2628
private ExperimentalSnippetHolder() {}

0 commit comments

Comments
 (0)