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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:45:51+00:00 2026-05-28T05:45:51+00:00

I have a methode foo in base class uses Synchronized (class name) , and

  • 0

I have a methode foo in base class uses Synchronized (class name) , and two classes A and B that extends the base class. if i called foo from A instance and B instance in two different thread are they gonna be Synchronized. here’s a sample code :

class BaseClass { 
        void foo() {
        synchronized(BaseClass.class)   
            // do something like increment count 
        }   
    }


    class A extends BaseClass {
    }


    class B extends BaseClass {
    }

    A a = new A(); 
    B b = new B();
    //in thread 1
    a.foo() ; 
    //in thread 2
    b.foo() ;
  • 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-28T05:45:52+00:00Added an answer on May 28, 2026 at 5:45 am

    Yes, that will be synchronized across all instances of all classes extending BaseClass (including BaseClass itself). The BaseClass.class reference will basically be a single reference for the whole classloader. Do you really want that?

    Usually, when synchronization is required, static methods should synchronize on something static, and instance methods should synchronize on something related to the instance. Personally I don’t like synchronizing on either this or a Class reference – as both of those references are available elsewhere, so other code could synchronize on the same monitor, making it hard to reason about the synchronization. Instead, I would tend to have:

    public class Foo {
        private final Object instanceLock = new Object();
    
        public void doSomething() {
            synchronized (instanceLock) {
                // Stuff
            }
        }
    }
    
    public class Bar {
        private static final Object staticLock = new Object();
    
        public static void doSomething() {
            synchronized (staticLock) {
                // Stuff
            }
        }
    }
    

    (I typically actually just use lock as the name; I’ve just made it more explicit here for clarity.)

    • 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 foo that will be used in multiple child classes
I have a set of classes that inherit from the same base class and
I have a class called Foo that looks like this: class Foo < ActiveRecord::Base
I have a base class Foo that is concrete and contains 30 methods which
I have written some code that uses attributes of an object: class Foo: def
I have a base class Foo that has an Update() function, which I want
I have a base class called Object. PhysicsObject inherits from Object. Ball inherits from
i have the following classes. class base { private function __construct(){ //check if foo
Newbie Python question. I have a class that inherits from several classes, and some
In my code I have a base class Foo and all my objects inherits

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.