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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:39:04+00:00 2026-06-05T22:39:04+00:00

What is the preferred way to work with Singleton class in multithreaded environment? Suppose

  • 0

What is the preferred way to work with Singleton class in multithreaded environment?

Suppose if I have 3 threads, and all of them try to access getInstance() method of singleton class at the same time –

  1. What would happen if no synchronization is maintained?
  2. Is it good practice to use synchronized getInstance() method or use synchronized block inside getInstance().

Please advise if there is any other way out.

  • 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-05T22:39:06+00:00Added an answer on June 5, 2026 at 10:39 pm

    The task is non-trivial in theory, given that you want to make it truly thread safe.

    A very nice paper on the matter is found @ IBM

    Just getting the singleton does not need any sync, since it’s just a read. So, just synchronize the setting of the Sync would do. Unless two treads try to create the singleton at start up at the same time, then you need to make sure check if the instance is set twice (one outside and one inside the sync) to avoid resetting the instance in a worst case scenario.

    Then you might need to take into account how JIT (Just-in-time) compilers handle out-of-order writes. This code will be somewhat near the solution, although won’t be 100% thread safe anyway:

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized(Singleton.class) {      
                Singleton inst = instance;         
                if (inst == null) {
                    synchronized(Singleton.class) {  
                        instance = new Singleton();               
                    }
                }
            }
        }
        return instance;
    }
    

    So, you should perhaps resort to something less lazy:

    class Singleton {
        private static Singleton instance = new Singleton();
    
        private Singleton() { }
    
        public static Singleton getInstance() {
            return instance;
        }
    }
    

    Or, a bit more bloated, but a more flexible way is to avoid using static singletons and use an injection framework such as Spring to manage instantiation of "singleton-ish" objects (and you can configure lazy initialization).

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

Sidebar

Related Questions

Suppose we have created Entities model, what is preferred way to work with it?
What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites
What is the preferred way to build application gui components tree? Instantiate all components
Which is the preferred way of defining class properties in Python and why? Is
Is there a (or, do you have your own) preferred way to do background
I have a custom PageBase class that I am using for all of the
Does anyone have any suggestions for a best practice or preferred way of rolling
If I have a ConcurrentQueue, is there a preferred way to consume it with
What’s the preferred way to handle 404 errors with Play 2.0 and show a
So what will be the preferred way of initializing records? With a 'factory function':

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.