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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:35:59+00:00 2026-06-11T23:35:59+00:00

I have a threading, singleton, android question. So let’s say we have the following

  • 0

I have a threading, singleton, android question.

So let’s say we have the following singleton code.

public class Singleton {
    private Singleton instance;
    private int number1 = 0;

    private Singleton() {
        //lots of initialization code
    }

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

My question. Would access to number1 be threadsafe or would I need to create an accessor with a locking mechanism?

Thanks for the help.

  • 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-11T23:36:00+00:00Added an answer on June 11, 2026 at 11:36 pm

    Your code won’t compile, because instance is not static. The declaration should read:

    private static Singleton instance;
    

    Other than that, you’ve set up a good mechanism to ensure that your application only has one instance of Singleton.

    Doing this however, does not safeguard your member variable number1 in any way. If you access that member variable from multiple threads, you’ll still need to synchronize things properly.

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

Sidebar

Related Questions

Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
let's say i have a singleton object with a static function: static int MySingletonObject::getInt()
I have following code: mCommandMap[command.pId] = new System.Threading.ManualResetEvent(false); SendCommandASync(command); mCommandMap[command.pId].WaitOne(); Where mCommandMap is a
I have a java threading code. I have 3 threads, I have given names
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
I have a Sender class that sends a Message on a IChannel : public
I'm looking to have a class (subclassed from threading.Thread) that is initialised and started
I'm relatively new to VB.NET and WPF and I have a basic threading question.
I have extended threading.Thread - my idea is to do something like this: class
I'm not sure if I have a threading issue here or not. On the

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.