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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:41:11+00:00 2026-06-02T18:41:11+00:00

My architect always says that Never synchronize on Boolean I am not able to

  • 0

My architect always says that

Never synchronize on Boolean

I am not able to understand the reason why and would really appreciate if someone could explain with an example as to why it is not a good practice.
Reference Sample Code

private Boolean isOn = false;
private String statusMessage = "I'm off";
public void doSomeStuffAndToggleTheThing(){

   // Do some stuff
   synchronized(isOn){
      if(isOn){
         isOn = false;
         statusMessage = "I'm off";
         // Do everything else to turn the thing off
      } else {
         isOn = true;
         statusMessage = "I'm on";
         // Do everything else to turn the thing on
      }
   }
}
  • 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-02T18:41:15+00:00Added an answer on June 2, 2026 at 6:41 pm

    I am not able to understand the reason why we should "never synchronize on Boolean"

    You should always synchronize on a constant object instance. If you synchronized on any object that you are assigning (i.e. changing the object to a new object) then it is not constant and different threads will be synchronizing on different object instances. Because they are synchronizing on different object instances, multiple threads will be entering the protected block at the same time and race conditions will happen. This is the same answer for synchronizing on Long, Integer, etc..

    // this is not final so it might reference different objects
    Boolean isOn = true;
    ...
    synchronized (isOn) {
       if (isOn) {
          // this changes the synchronized object isOn to another object
          // so another thread can then enter the synchronized with this thread
          isOn = false;
    

    To make matters worse, any Boolean that is created through autoboxing (isOn = true) is the same object as Boolean.TRUE (or .FALSE) which is a singleton in the ClassLoader across all objects. Your lock object should be local to the class it is used in otherwise you will be locking on the same singleton object that other classes might be locking on in other lock cases if they are making the same mistake.

    The proper pattern if you need to lock around a boolean is to define a private final lock object:

    private final Object lock = new Object();
    ...
    
    synchronized (lock) {
       ...
    

    Or you should also consider using the AtomicBoolean object which means you may not have to synchronize on it at all.

    private final AtomicBoolean isOn = new AtomicBoolean(false);
    ...
    
    // if it is set to false then set it to true, no synchronization needed
    if (isOn.compareAndSet(false, true)) {
        statusMessage = "I'm now on";
    } else {
        // it was already on
        statusMessage = "I'm already on";
    }
    

    In your case, since it looks like you need to toggle it on/off with threads then you will still need to synchronize on the lock object and set the boolean and avoid the test/set race condition:

    synchronized (lock) {
        if (isOn) {
            isOn = false;
            statusMessage = "I'm off";
            // Do everything else to turn the thing off
        } else {
            isOn = true;
            statusMessage = "I'm on";
            // Do everything else to turn the thing on
        }
    }
    

    Lastly, if you expect the statusMessage to be accessed from other threads then it should be marked as volatile unless you will synchronize during the get as well.

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

Sidebar

Related Questions

I am a C# .NET developer/architect and understand that it uses objects (.NET objects)
I built a really basic php/mysql site for an architect that uses one 'projects'
I'm having a hard time convincing our architect that a Domain model should only
Say I've been tasked to architect a product that will have a browser-based front
Unfortunately my Enterprise Architect 7.5 does not export in SVG, only in EMF (if
I always use Enterprise Architect to generate the class diagram of a C++ project
As a database architect, developer, and consultant, there are many questions that can be
I've been attempting to architect a server side API that runs using a noSQL
I need to re-architect a PHP application that is entirely procedural. Pretty much every
Does anyone know an application (Enterprise Architect, ...) that allows mapping from UML Class

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.