-
Notifications
You must be signed in to change notification settings - Fork 0
Thread Safety
frosxt edited this page Jan 18, 2026
·
1 revision
By default, the standard JsonConfiguration is not thread-safe, similar to HashMap. If multiple threads try to read/write to the same config simultaneously, you may encounter ConcurrentModificationException or an inconsistent state.
The library provides a wrapper class ConcurrentJsonConfiguration that handles locking automatically.
Wrap your existing configuration:
import com.github.frosxt.jsonconfig.api.JsonConfiguration;
import com.github.frosxt.jsonconfig.api.concurrent.ConcurrentJsonConfiguration;
JsonConfiguration baseConfig = JsonConfiguration.loadConfiguration(file);
JsonConfiguration threadSafeConfig = new ConcurrentJsonConfiguration(baseConfig);You can now use threadSafeConfig exactly like a normal configuration. It implements the same interface but uses ReadWriteLock internally.
- Reads (getters) use a shared Read Lock. Multiple threads can read at the same time.
- Writes (setters) use an exclusive Write Lock. Only one thread can write at a time, blocking readers/writers.
- Async Saving: You can safely save the configuration to a file in a background thread while the main thread continues to read from it.
- Web Applications: Safe to share a global config instance across multiple request threads.
There is a small overhead due to locking. For single-threaded applications, use the standard JsonConfiguration.