-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean WeakConcurrentMap from background thread (#6240)
Currently our `WeakConcurrentMap` is only cleaned of stale entries when it is accessed. There is an option to clean from a background thread, but this creates a separate thread for every map. This pr introduces a single background thread that cleans all maps. I removed the option to create a thread per map as we don't use it, if there is interest I could attempt to find a way to add it back. Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
- Loading branch information
Showing
4 changed files
with
72 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...entelemetry/instrumentation/api/internal/cache/weaklockfree/WeakConcurrentMapCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.internal.cache.weaklockfree; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public final class WeakConcurrentMapCleaner { | ||
private static Thread thread; | ||
|
||
private WeakConcurrentMapCleaner() {} | ||
|
||
public static synchronized void start() { | ||
if (thread != null) { | ||
return; | ||
} | ||
|
||
thread = new Thread(AbstractWeakConcurrentMap::runCleanup, "weak-ref-cleaner"); | ||
thread.setDaemon(true); | ||
thread.setContextClassLoader(null); | ||
thread.start(); | ||
} | ||
|
||
@SuppressWarnings("Interruption") | ||
public static synchronized void stop() { | ||
if (thread == null) { | ||
return; | ||
} | ||
|
||
thread.interrupt(); | ||
thread = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters