Can we set more than one object in a ThreadLocal ?
A thread local is a local variable of the current thread; so each thread gets exactly one value. But the value can be an instance, so you can put a map in there, for example or a custom type which collects all the values that you want.
You can have multiple ThreadLocal and you can have an object in it which contains multiple objects.
e.g.
final ThreadLocal<Map<String, String>> localProperties = new ThreadLocal<Map<String, String>>() {
     public Map<String, String> initialValue() {
           return new LinkedHashMap<String, String>();
     }
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With