See: Description
| Interface | Description |
|---|---|
| ConcurrentCommentedConfig |
Interface for thread-safe configurations with comments.
|
| ConcurrentConfig |
Interface for thread-safe configurations.
|
| Class | Description |
|---|---|
| StampedConfig |
A thread-safe configuration based on a
StampedLock. |
| StampedConfig.Accumulator |
A CommentedConfig that allows to quickly accumulate values before a
StampedConfig.replaceContentBy(Accumulator). |
| SynchronizedConfig |
A configuration that is synchronized, and therefore thread-safe (reads and
writes can happen in any order from any thread).
|
core package such as
Config.inMemory(),
are not thread-safe. It is wrong to use them from multiple threads.
Even when a thread-safe Map is used to store the config's values, as that is the case with
Config.inMemoryConcurrent(),
there is no way to perform multiple operations in a consistent way, because their order is not guaranteed
and they can overlap each other.
On top of that, using sub-configurations in a single call, such as config.set("a.b.c", x) is
problematic, because each subconfig has its own Map,
the comments and the values are stored separately, and there is no mechanism that ensures the consistency
of the whole configuration.
For instance, if a thread A executes
config.set("a.b", "value");
String a = config.get("a.b")
and another thread B executes
String b = config.remove("a.b");
it is possible that thread A gets a = null, or that thread B gets b = null, or that they
both get a value!
This problem is even worse with complex operations and can lead to incorrect results or corrupted
configurations.
ConcurrentConfig (and its commented version
ConcurrentCommentedConfig).
Classes that implement ConcurrentConfig offer the
following features and guarantees:
get and set methods.ConcurrentConfig#bulkRead(java.util.function.Function),
ConcurrentConfig#bulkUpdate(java.util.function.Function),
ConcurrentCommentedConfig#bulkCommentedRead(java.util.function.Function)
and
ConcurrentCommentedConfig#bulkCommentedUpdate(java.util.function.Function).
ConcurrentConfig config = new SynchronizedConfig();
List<String> newPlayerList = config.bulkUpdate(conf -> {
List<String> playerNames = conf.get("players");
playerNames.add("NewPlayer");
playerNames.remove("BadPlayer");
conf.set("players", playerNames);
return playerNames;
});
Some configurations like StampedConfig and SynchronizedConfig also provide a way to atomically
replace their entire content. At the moment, this feature is not part of the ConcurrentConfig interface.
config
object must not be used in the function given to bulkRead and bulkUpdate.
Conversely, the view must not be used outside of the bulk operation.
SynchronizedConfig#replaceContentBy(com.electronwill.nightconfig.core.Config)
for more information.