How Locale could be used in multithreaded application to improve performance

After short exploring it looks like JDK can't help you. I suggest get java.lang.ConditionalSpecialCasing class, copy it and fix problem with Hashtable. You may replace Hashtable with HashMap. I do not see any reason for using Hashtable here.


Edit: The solution bellow won't actually work, because the problematic HashTable in the java.lang.ConditionalSpecialCasing class is static and will still be shared by all threads. I suggest that you accept sibnick's answer instead of mine.


One simple solution would be to make trLoc a ThreadLocal: a new instance will be automatically created for each thread (as needed). This will work fine if you have a thread pool or similar: you will only create as many instances of Locale as you have threads in your pool, which should be quite reasonable. And since each thread will access a different instance of Locale, you will no longer have contention to access the synchronized HashTable.

private ThreadLocal<Locale> trLoc = new ThreadLocal<Locale>() {
    @Override
    protected Locale initialValue() {
        return new Locale("tr", "TR");
    }
};

public double calculate(String arg1){
    arg1 = arg1.toUpperCase(trLoc.get());
    ...
}