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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:05:38+00:00 2026-05-27T22:05:38+00:00

This is a successor of my previous question, Is this variable being safely accessed

  • 0

This is a successor of my previous question, Is this variable being safely accessed by using synchronization?

For the following program,

Class SubClassB extends SuperClassA {
     protected int c;

     public void inc() {
          synchronized (this) {
               c++;
          }
     }

     public void dec() {
          synchronized ( (SuperClassA) this) {
               c--;
          }
     }
}

Would the counter “c” be accessed thread safe ? I am not sure that in the “dec()” method, is the SuperClassA cast “this” reference a valid object for the synchronized block ? If yes, would the two synchronized blocks lock the same “this” object ? (As it seems to me that “(SuperClassA) this” is not equal to “this”)

This strange simulated code comes from the following real life example where SuperClassA is a base class that is not supposed to be modified,

Class SuperClassA {
     protected int c;

     public void dec() {
          synchronized (this) {
               c--;
          }
     }
}

Class SubClassB extends SuperClassA {

     public void inc() {
          synchronized (this) {
               c++;
          }
     }

     public void dec() {
          super.dec();
     }
}

In this example, the “dec()” method in SubClassB calls its superclass’s “dec()” method which performs a lock to a “this” object that I suppose to be “SuperClassA.this”. If the locked object in SubClassB’s “inc()” method is not exactly the same as the locked object in SubClassB’s “dec()” method, then I am wondering the inherited counter “c” in SubClassB may NOT be safely accessed by different threads. I feel that there is some ambiguities in using the “this” reference in synchronized blocks.

In the real life example, if I want SubClassB’s counter “c” to be thread safe, do I need to add one more synchronized block in its “dec()” method, like this,

Class SubClassB extends SuperClassA {

     public void inc() {
          synchronized (this) {
               c++;
          }
     }

     public void dec() {
          synchronized (this) {
               super.dec();
          }
     }
}

But it seems that such added block is not elegant and may be redundant !

Does anyone have some ideas on these questions. Thanks in advance.

Lawrence

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

    Would the counter “c” be accessed thread safe ?

    Yes, it’s using the same lock object.

    I am not sure that in the “dec()” method, is the SuperClassA cast “this” reference a valid object for the synchronized block ?

    Yes.

    If yes, would the two synchronized blocks lock the same “this” object ? (As it seems to me that “(SuperClassA) this” is not equal to “this”)

    Yes. Even if you cast the instance to something it can be casted to (even Object), it’ll still refer to the same object.

    […] But it seems that such added block is not elegant and may be redundant !

    It is redundant. Extra synchronization is required only if you call multiple synchronized methods and the combined effect must be atomic.

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

Sidebar

Related Questions

This is closely related to my previous question , which was about using CMake
This is a follow up to my previous question about using XMLHttpRequest() to post
This is something of a follow up from a previous question . The requirements
This is a follow up to a previous question that I had before about
Before you -1 this for being such a simple question read further, because ive
Following on from my previous question [link text][1] , I have a new problem.
This is related to my previous question , but it seems I have another
This is a follow-up to my previous question on pretty-printing STL containers , for
Following on from this question , I now want to know how to stop
this is related to my previous question about jqgrid. im doing now a search

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.