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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:51:39+00:00 2026-05-24T10:51:39+00:00

I have a class as follows public MyClass{ Boolean flag = false; public Boolean

  • 0

I have a class as follows

public MyClass{

Boolean flag = false;

    public Boolean getflag(){
       synchronized(flag){
          //BLOCK 1
          return flag;
       }
    }

    public Boolean setflag(){
       synchronized(flag){
           //BLOCK 2
           this.flag = flag;
       }
    }
}

Both methods are synchronized on object flag.Now my doubt is can two different threads can simultaniously executed the synchronized blocks (1&2).
Can the following situation arise?
1)Thread 1 is setting flag value and thread 2 getting its value at same time?

  • 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-24T10:51:40+00:00Added an answer on May 24, 2026 at 10:51 am

    Yes it can. Note that you are synchronizing on the same object you are setting. So the setter can change the object reference to something different, then the getter can synchronize on the new object while the setter is still inside the synchronized block.

    But there is more: flag is (usually) a reference to one of the system-wide singletons Boolean.TRUE and Boolean.FALSE, so it is (at least theoretically) possible to lock on these outside of your class, even without referring to your class in any way. In which case you can end up with a deadlock and you can have a hard time figuring out why.

    (Note also that the code in its current form is wrong, since the setter has no parameter, thus this.flag = flag assigns the reference to itself – but above I assume that you meant it to behave like a normal setter 🙂

    The fix is to use a dedicated, private final lock object (in case you want to absolutely ensure that noone outside can synchronize on the same lock you are using within your class – which I suppose your original intention was):

    public MyClass{
    
        private final Object lock = new Object();
        private Boolean flag = false;
    
        public Boolean getflag(){
           synchronized(lock){
              //BLOCK 1
              return flag;
           }
        }
    
        public void setflag(Boolean flag){
           synchronized(lock){
               //BLOCK 2
               this.flag = flag;
           }
        }
    }
    

    If you aren’t worried so much about other synchronizing on the same lock you internally use, you can simply make your methods synchronized (in which case they lock on this).

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

Sidebar

Related Questions

I have got a template class as follows: class MyClass<T> { T field; public
I have a class with a delegate declaration as follows... Public Class MyClass Public
I have 2 methods in C++ class as follows: class myClass { public: void
I have two Java-Classes as follows: public class MyClass { ... } public class
I have a class like this: public class MyClass { private Queue<MyOtherClass> myQueue; }
So I have this class public static class MyClass { static MyClass() { ...
So I have a class with a method as follows: public class SomeClass {
I have a class A as follows: class A { public: A() { printf(A
I have a controller with an action method as follows: public class InventoryController :
I have two classes Address and Employee as follows: public class Address { public

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.