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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:58:33+00:00 2026-06-10T03:58:33+00:00

I am not clear the concept of Java Thread acquires an object level lock

  • 0

I am not clear the concept of

Java Thread acquires an object level lock when it enters into an instance synchronized java method and acquires a class level lock when it enters into static synchronized java method.

What does it mean When it says object level lock and class level lock ?

For example:

 public class Counter{
      private static int count = 0;
      private int count2 = 0;

      public static synchronized int getCount(){
           return count;
      }
      public synchronized setCount(int count2){
           this.count2 = count2;
      }
 } 

getCount() here will lock Counter.class object while setCount() will lock on current object(this). What does that this refer to ? Does that mean when getCount() get called another thread can’t access setCount() since the whole class is locked ?

  • 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-10T03:58:34+00:00Added an answer on June 10, 2026 at 3:58 am

    What does it mean When it says object level lock and class level lock ?

    When you lock on a static method you are locking on the Class object itself and there is one of these per ClassLoader. In your example,

    public static synchronized int getCount(){
    

    This is locking on the Counter.class object and is the same as:

    public static int getCount() {
        synchronized (Counter.class) {
        }
    

    If you are instead locking on a method that is not static then you are locking on the instance of the object that owns that method. In your example:

    public synchronized void setCount(int count){
    

    This is the same as locking on the particular Counter instance and is equivalent to:

    public void setCount(int count){
        synchronized (this) {
           ...
    

    So if you have 2 Counter objects, counter1 and counter2, and 1 thread is calling counter1.getCount() and the other is calling counter2.getCount() at the same time, then they will both lock on the same Class object and one will block the other.

    But if the 2 threads are instead calling counter1.setCount(...) and counter2.setCount() they will be locking on different objects — counter1 and counter2 respectively. They will not block each other.

    As mentioned, it is very bad form to have asymmetry on your setters and getters and it is unusual to have either be static.

    Does that mean when getCount() get called another thread can’t access setCount() since the whole class is locked ?

    No. If getCount() is called, the Counter.class is locked and when setCount(...) is called counter1 or counter2 is locked. The only time a lock blocks a thread is when the same object has been locked by another thread. Just because there is a lock on Counter.class does not mean that there is some sort of uber-class lock. The only time that will block another thread is if it too locks on Counter.class.

    I’d take a moment to read Sun’s great documentation on how synchronized works.

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

Sidebar

Related Questions

I am not clear on the actual difference between these two styles of class
With solid C++ in my back some Java concepts are not 100% clear. List<SomeInterface>
I've came accross this concept several times and did not find a clear explanation.
Not clear on some fundamental syntax here. define-key accepts a set of inputs, one
It's not clear, to me anyway, if a UIView is automatically released from its
It's not clear to me if the Eclipse plugin completely replaces the need to
I'm not clear on the status of XHTML - v1.0 versus v1.1. Can someone
I am not clear from the documentation - when I indicate a cell width
Sorry if question is not clear. My vocabulary for terminology is bad. Anyways I
My question is not clear at title [i can not write it exactly] e.g

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.