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

  • Home
  • SEARCH
  • 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 7770065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:19:01+00:00 2026-06-01T16:19:01+00:00

Which one among ThreadLocal or a local variable in Runnable will be preferred? For

  • 0

Which one among ThreadLocal or a local variable in Runnable will be preferred? For performance reasons. I hope using a local variable will give more chances for cpu caching, etc.

  • 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-06-01T16:19:02+00:00Added an answer on June 1, 2026 at 4:19 pm

    Which one among ThreadLocal or a local variable in Runnable will be preferred.

    If you have a variable that is declared inside the thread’s class (or the Runnable) then a local variable will work and you don’t need the ThreadLocal.

    new Thread(new Runnable() {
        // no need to make this a thread local because each thread already
        // has their own copy of it
        private SimpleDateFormat format = new SimpleDateFormat(...);
        public void run() {
           ...
           // this is allocated per thread so no thread-local
           format.parse(...);
           ...
        }
    }).start();
    

    On the other hand, ThreadLocals are used to save state on a per thread basis when you are executing common code. For example, the SimpleDateFormat is (unfortunately) not thread-safe so if you want to use it in code executed by multiple threads you would need to store one in a ThreadLocal so that each thread gets it’s own version of the format.

    private final ThreadLocal<SimpleDateFormat> localFormat =
        new ThreadLocal<SimpleDateFormat>() {
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(...);
            }
         };
    ...
    // if a number of threads run this common code
    SimpleDateFormat format = localFormat.get();
    // now we are using the per-thread format (but we should be using Joda Time :-)
    format.parse(...);
    

    An example of when this is necessary is a web request handler. The threads are allocated up in Jetty land (for example) in some sort of pool that is outside of our control. A web request comes in which matches your path so Jetty calls your handler. You need to have a SimpleDateFormat object but because of its limitations, you have to create one per thread. That’s when you need a ThreadLocal. When you are writing reentrant code that may be called by multiple threads and you want to store something per-thread.

    Instead, if you want pass in arguments to your Runnable then you should create your own class and then you can access the constructor and pass in arguments.

    new Thread(new MyRunnable("some important string")).start();
    ...
    private static class MyRunnable implements {
        private final String someImportantString;
        public MyRunnable(String someImportantString) {
            this.someImportantString = someImportantString;
        }
        // run by the thread
        public void run() {
           // use the someImportantString string here
           ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which one is preferable or more clear? public int FrozenRegionWidth { get; set; }
which one will be better to use default magic quotes or user defined addslash/stripslash
for gcc they should be the same, right? which one of them is more
Integer.toString(int); and String.valueOf(int); Which one among the above two methods is the efficient way
Among all the encodings available here http://docs.python.org/library/codecs.html which one is the one I should
Which one is better among these three? string myString = ; String.IsNullOrEmpty(myString); vs string
I'm lost among the zillion versions of VMware. Which one should I choose? I
Which one among the two languages is good for statistical analysis? What are the
I made this bash one-liner which I use to list Weblogic instances running along
Supposing I have one perl in /usr/bin (which came along with my os-distribution) and

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.