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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:16:22+00:00 2026-06-18T12:16:22+00:00

From Java Concurrency in practice Chapter 3.3.3. ThreadLocal Thread-local variables are often used to

  • 0

From Java Concurrency in practice Chapter 3.3.3. ThreadLocal

Thread-local variables are often used to prevent sharing in designs
based on mutable Singletons or global variables.

If we wrap the mutable Singleton guy in a ThreadLocal each thread will have its own copy of the Singleton ? How will it remain a singleton then ? Is this what the authors meant or am I missing something pretty obvious here ?

  • 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-18T12:16:23+00:00Added an answer on June 18, 2026 at 12:16 pm

    If we wrap the mutable Singleton guy in a ThreadLocal

    AFAIK you do not wrap the singleton class with ThreadLocal, but the object contained within the singleton which is mutable or non-thread safe. As the example discusses correctly that the JDBC Connection is not thread safe and will require additional protection, which in turn increases contention.

    So in the cases when the Singletons are used just for the purpose of sharing, then replacing those things with ThreadLocal is a good idea, as all the threads have their own Connection and no more added protection is required.

    Other good example of use case of ThreadLocal is Random generation, if there is a single Random object then there is contention within threads for the “seed”, so if each thread has its own Random object then there is no contention any more and that makes sense.

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

Sidebar

Related Questions

From book Java Concurrency in Practice page 26: You can use volatile variables only
From the famous book Java Concurrency in Practice chapter 3.4.1 Final fields Just as
I am reading Java Concurrency in Practice and kind of confused with the thread
// code exactly taken from java concurrency in practice public class TimingThreadPool extends ThreadPoolExecutor
//code taken from java concurrency in practice package net.jcip.examples; import java.util.concurrent.*; public class ThreadDeadlock
This is straight from Java Concurrency in Practice. The following causes a Deadlock :
This code snippet from java concurrency in practice, I really don't understand. @ThreadSafe public
From Effective Java ( Chapter 10 Concurrency ) Suppose you were to invoke an
Code from the book Java Concurrency in Practice Listing 8.1 Why is the code
I am reading Java concurrency in practice and the below examples are from that.

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.