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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:02:48+00:00 2026-05-27T17:02:48+00:00

As per the singleton pattern, public sealed class Singleton { static Singleton instance=null; Singleton()

  • 0

As per the singleton pattern,

public sealed class Singleton
{
    static Singleton instance=null;

    Singleton()
    {
    }

    public void abc(){
    }

    public static Singleton Instance
    {
        get
        {
            if (instance==null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

the above is not thread-safe.Two different threads could both have evaluated the test if (instance==null) and found it to be true, then both create instances, which violates the singleton pattern.

Confusion is Instance is static, how this can be null once it is called on UI thread or other threads?

EDIT

I meant to said that once i have called Singleton.Instance.abc();
Singleton.Instance should not be null until it is disposed manually. Right?

  • 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-27T17:02:49+00:00Added an answer on May 27, 2026 at 5:02 pm

    Control is passed to ThreadA

    ThreadA trys to get the Instance, it is found to be null.

    Control is passed to ThreadB

    ThreadB trys to get the Instance, it is found to be null.

    Control is passed to ThreadA

    ThreadA instantiates Instance.

    Control is passed to ThreadB

    ThreadB re-instantiates Instance.

    Solution: You an use a static constructor to ensure this doesn’t happen.

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

Sidebar

Related Questions

If we have a singleton class like LoadBalancer and need one instance per ASP.NET
Suppose I have a Singleton class (any class can get the instance): class data
How many number of Instance can be there when same singleton class is packaged
As per my thoughts, we should make a class as Singleton when we share
I'm looking for a design\pattern to my problem. I want to declare a singleton
What are the scenarios when one would use a Monostate pattern instead of singleton
I wonder what scopes folowing two bindings have: bind(PermissionManager.class).in(Singleton.class); and bind(PermissionManager.class); I have read
Imagine in the Global.asax.cs file I had an instance class as a private field.
I would like to create a singleton class that is instantiated once in each
Possible Duplicate: Efficient way to implement singleton pattern in Java I was reading this

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.