Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7616527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:54:37+00:00 2026-05-31T02:54:37+00:00

Do Java ThreadLocal variables produce thread-local values if they are used as instance variables

  • 0

Do Java ThreadLocal variables produce thread-local values if they are used as instance variables (e.g., in a method that generates thread-local objects), or must they always be static to do so?

As an example, assume a typical scenario where several, expensive to initialize objects of a class that is not thread-safe, need to be instantiated in a single static initialization block, stored in static variables of a single class (e.g., in a Map data structure) and from then on used for intensive processing by numerous different threads.

To achieve thread safety, obviously a different copy of each static object must be passed. For instance, Java DateFormat objects that need to be safely used across different threads.

In many examples one can find on the web, the approach seems to be declaring separately each ThreadLocal variable, instantiate the new object in the initialValue() method and then use the get() method to retrieve a thread-local instance.

This approach is not very efficient if there are dozens or hundreds of such objects to be created, each with its own initialization parameters. For example, many SimpleDateFormat objects with a different date pattern each.

If the instantiation of the objects could be done in a loop which produces a different value in each iteration, a generic method for producing the thread-local instances would be needed, after each value is created by properly initializing the corresponding object.

Based on the above, the following generic static method would not work, because the same reference is produced on every call to initialValue():

// Each value is an object initialized prior to calling getLocal(...)
public static final <T> T getLocal(final T value)
{
    ThreadLocal<T> local = new ThreadLocal<T>()
    {
        @Override
        protected T initialValue()
        {
            return value;
        }
    };

    return local.get();
}

Instead, a mechanism for creating a new object inside initialValue() is needed. So, the only generic approach is probably using reflection, in a pattern similar to

private static final <T> T getLocal(
        final Constructor<T> constructor, final Object[] initargs)
{
    ThreadLocal<T> local = new ThreadLocal<T>()
    {           
        @Override
        protected T initialValue()
        {
            T value = null;

            try // Null if the value object cannot be created
            {
                value = constructor.newInstance(initargs);
            }
            catch (Exception e)
            {
            }

            return value;
        }
    };

    return local.get();
}

Then, of course, there is the type-specific option, where one could just use the ThreadLocal pattern in the loop for declaring each variable.

For example, in the case of DateFormat, in a single, static initialization block, one could do

private static String[] patterns = ... // Get date patterns
private static DateFormat format;

public static Map<String, DateFormat> formats = new HashMap<String, DateFormat>();

static
{
    for (final String pattern:patterns)
    {
        format = new ThreadLocal<DateFormat>()
        {           
                @Override
            protected DateFormat initialValue()
                {
            return new SimpleDateFormat(pattern);
            }
        }.get();

        formats.put(pattern, format);
}

From then on, the formats map will be read by different classes, across different threads, each time in order to invoke the format() or parse() method of one or more DateFormat objects stored in the map.

Does any of the above approaches make sense for the case described, or should the ThreadLocal declarations be static?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:54:38+00:00Added an answer on May 31, 2026 at 2:54 am

    Do Java ThreadLocal variables produce thread-local values if they are used as instance variables.

    Yes, they do. Think about it: Not the ThreadLocal is static or non-static, only the reference to the ThreadLocal is static or not. The object itself looks always the same.

    Does any of the above approaches make sense for the case described, or should the ThreadLocal declarations be static?

    Not really.

    Example:

    [DateFormat] format = new ThreadLocal<DateFormat>()
        {...}.get();
    formats.put(pattern, format);
    

    means, that you always create a new ThreadLocal, call get immediately and put the result (not the ThreadLocal) into a map. This means you neither reuse the ThreadLocal nor the format itself.

    so, as far as I understand your usecase you might want something like this:

    public class XXX {
        private final static Map<String, SimpleDateFormatThreadLocal> formatMap = 
            new HashMap<String, SimpleDateFormatThreadLocal>();
    
        static {
            String[] patterns = {"a", "b", "c"};
            for(String pattern: patterns){
                formatMap.put(pattern, new SimpleDateFormatThreadLocal(pattern));
            }
        }
    
        private static class SimpleDateFormatThreadLocal extends ThreadLocal<SimpleDateFormat> {
            private final String pattern;
    
            public SimpleDateFormatThreadLocal(String pattern) {
                this.pattern = pattern;
            }
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(pattern);
            }
        }
    }
    

    Example usage would be like this:

    public void run(){
        String s = formatMap.get("a").get().format(new Date());
        System.out.println(s);
    }
    

    Here you

    • reuse the ThreadLocal objects
    • reuse the DateFormat objects (per thread of course)
    • avoid creating DateFormats which are not used in some threads.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Conventional wisdom tells us that high-volume enterprise java applications should use thread pooling in
Java is nearing version 7. It occurs to me that there must be plenty
java.util.PriorityQueue allows a Comparator to be passed at construction time. When inserting elements, they
Java's checked exceptions sometimes force you to catch a checked exception that you believe
Java - How do you read binary objects into an object array without knowing
In Java, I have a Thread A . If A spawns a child thread,
I was reading the JavaDoc for Threadlocal here https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ThreadLocal.html and it says ThreadLocal instances
I'm using ThreadLocal variables (through Clojure's vars, but the following is the same for
When Using a thread-local database connection, closure of the connection is required when the
Java has a convenient split method: String str = The quick brown fox; String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.