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 8191867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:01:13+00:00 2026-06-07T04:01:13+00:00

The well acclaimed book JCIP says this about ThreadLocal usage : It is easy

  • 0

The well acclaimed book JCIP says this about ThreadLocal usage :

It is easy to abuse ThreadLocal by treating its thread confinement property as a license to use global variables or as a means of creating “hidden” method arguments.
Thread-local variables can detract from reusability and introduce hidden couplings among classes, and should therefore be used with care.

What does it mean by saying that Thread-local variables can reduce reusability and introduce hidden couplings among classes?

  • 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-07T04:01:15+00:00Added an answer on June 7, 2026 at 4:01 am

    They reduce reusability in much the same way that global variables do: when you method’s computations depend on state which is external to the method, but not passed as parameters (i.e. class fields for example), your method is less reusable, because it’s tightly coupled to the state of the object/class in which it resides (or worse, on a different class entirely).

    Edit: Ok, here’s an example to make it more clear. I’ve used ThreadLocal just for the sake of the question, but it applies to global variables in general. Assume I want to calculate the sum of the first N integers in parallel on several threads. We know that the best way to do it is to calculate local sums for each thread and them sum them up at the end. For some reason we decide that the call method of each Task will use a ThreadLocal sum variable which is defined in a different class as a global (static) variable:

    class Foo {
        public static ThreadLocal<Long> localSum = new ThreadLocal<Long>() {
            public Long initialValue() {
                return new Long(0);         
            }
        };
    }
    
    class Task implements Callable<Long> {
    
        private int start = 0;
        private int end = 0;    
    
        public Task(int start, int end) {
            this.start = start;
            this.end = end;
        }
    
        public Long call() {
            for(int i = start; i < end; i++) {
                Foo.localSum.set(Foo.localSum.get() + i);
            }
            return Foo.localSum.get();
        }
    }
    

    The code works correctly and gives us the expected value of the global sum, but we notice that the class Task and its call method are now strictly coupled to the Foo class. If I want to reuse the Task class in another project, I must also move the Foo class otherwise the code will not compile.

    Although this is a simple example complicated on purpose, you can see the perils of “hidden” global variables. It also affects readability, since someone else reading the code will have to also search for the class Foo and see what the definition of Foo.localSum is. You should keep your classes as self-contained as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, i've got a nice WPF book its called Sams Windows Presentation Foundation Unleashed.
Well I can start the thread when I press togglebutton to ON and it
well i have a configuration like this in the components part of my config
Well, I was trying to fix this program, and I keep getting the errors
Well after much messing about I have finally got a query that gives sales
Well I'm making an program about payrolls and I'm stuck. In the program, after
Well, this is an interesting problem. I have an ASP.NET MVC3 Intranet application running
Well, I know there is a lots of posts about it, but I have
Well, I got problem here with opengl ES stuff (just started to learn about
well I'm starting to understand Android Fragments but this is still confusing for me.

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.