Make some things non-static
MODIFY
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/WeakMapSuppliers.java
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/WeakMapSuppliers.java
@@ -38,9 +38,6 @@ class WeakMapSuppliers {
private static final long CLEAN_FREQUENCY_SECONDS = 1;
private static final long SHUTDOWN_WAIT_SECONDS = 5;
- private static final Queue<WeakReference<WeakConcurrentMap>> SUPPLIED_MAPS =
- new ConcurrentLinkedQueue<>();
-
private static final ThreadFactory THREAD_FACTORY =
new ThreadFactory() {
@Override
@@ -55,12 +52,7 @@ class WeakMapSuppliers {
private static final ScheduledExecutorService CLEANER =
Executors.newScheduledThreadPool(1, THREAD_FACTORY);
- private static final Runnable RUNNABLE = new Cleaner();
-
static {
- CLEANER.scheduleAtFixedRate(
- RUNNABLE, CLEAN_FREQUENCY_SECONDS, CLEAN_FREQUENCY_SECONDS, TimeUnit.SECONDS);
-
try {
Runtime.getRuntime()
.addShutdownHook(
@@ -80,8 +72,24 @@ class WeakMapSuppliers {
}
}
- public static void cleanMaps() {
- for (final Iterator<WeakReference<WeakConcurrentMap>> iterator = SUPPLIED_MAPS.iterator();
+ private final Queue<WeakReference<WeakConcurrentMap>> suppliedMaps =
+ new ConcurrentLinkedQueue<>();
+
+ private final Runnable runnable =
+ new Runnable() {
+ @Override
+ public void run() {
+ cleanMaps();
+ }
+ };
+
+ WeakConcurrent() {
+ CLEANER.scheduleAtFixedRate(
+ runnable, CLEAN_FREQUENCY_SECONDS, CLEAN_FREQUENCY_SECONDS, TimeUnit.SECONDS);
+ }
+
+ public void cleanMaps() {
+ for (final Iterator<WeakReference<WeakConcurrentMap>> iterator = suppliedMaps.iterator();
iterator.hasNext(); ) {
final WeakConcurrentMap map = iterator.next().get();
if (map == null) {
@@ -95,18 +103,10 @@ class WeakMapSuppliers {
@Override
public <K, V> WeakMap<K, V> get() {
final WeakConcurrentMap<K, V> map = new WeakConcurrentMap<>(false);
- SUPPLIED_MAPS.add(new WeakReference<WeakConcurrentMap>(map));
+ suppliedMaps.add(new WeakReference<WeakConcurrentMap>(map));
return new Adapter<>(map);
}
- private static class Cleaner implements Runnable {
-
- @Override
- public void run() {
- cleanMaps();
- }
- }
-
private static class Adapter<K, V> implements WeakMap<K, V> {
private final WeakConcurrentMap<K, V> map;

MODIFY
dd-java-agent/agent-tooling/src/test/groovy/datadog/trace/agent/tooling/WeakConcurrentSupplierTest.groovy
dd-java-agent/agent-tooling/src/test/groovy/datadog/trace/agent/tooling/WeakConcurrentSupplierTest.groovy
@@ -16,11 +16,11 @@ class WeakConcurrentSupplierTest extends Specification {
def map1 = WeakMap.Provider.newWeakMap().map
then:
- supplier.SUPPLIED_MAPS.iterator().next().get() == map1
+ supplier.suppliedMaps.iterator().next().get() == map1
when:
def map2 = WeakMap.Provider.newWeakMap().map
- def iterator = supplier.SUPPLIED_MAPS.iterator()
+ def iterator = supplier.suppliedMaps.iterator()
then:
iterator.next().get() == map1

Make some things non-static