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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:16:06+00:00 2026-06-04T06:16:06+00:00

I have class A and B which inherit from Base. Base has a private

  • 0

I have class A and B which inherit from Base.

Base has a private method. It is possible that A and B execute this method at the same time.

So I want to synchronize that method. If I just put “synchronize” keyword in the signature, will that make that A and B execution is synchronized (share the same lock) ? Or do I need to generate a static lock object…?

Sorry, a bit confused…

  • 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-04T06:16:07+00:00Added an answer on June 4, 2026 at 6:16 am

    If your code looks a bit like the example below, then it is not thread safe and you need to include some form of synchronization to make sure the call to the private method increment is atomic and ensure visibility. In that simple example, making getNewCounter synchronized would be enough. Using a lock object would work too.

    public class Base {
        private int counter;
    
        private void increment() {
            counter++;
        }
        protected int getNewCounter() {
            increment();
            return counter;
        }
    }
    
    public class A extends Base {
        public int aMethod() {
            return getNewCounter();
        }
    }
    
    public class B extends Base {
        public int anotherMethod() {
            return getNewCounter();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class that I inherit from that has two zero to
I have 2 web user controls, both inherit the same base class which extends
So, I have a base class which has a private locking object like so:
I have usercontrols which inherit a base class which looks like this: BasePage.cs: using
I have two Parser classes that inherit from a base class, BaseParser. I want
I have a base class line which has a child class arc (note that
I have a class which I intend for others to inherit from. It has
I have a class Invoice which inherits from ActiveRecord::Base . Invoice has an attribute
I have my class X which inherits from Qt's class Base . I declared
I have a MustInherit Parent class with two Child classes which Inherit from 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.