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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:44:04+00:00 2026-06-10T09:44:04+00:00

When you have couple of synchronized blocks on an object (say) obj then how

  • 0

When you have couple of synchronized blocks on an object (say) obj then how does Java check if all these objs are the same or different?

For example:

public static f() {
    synchronized ("xyz") {
        ...
    }
}

If the above function f is called simultaneously by two threads will they block the other? Notice that each thread will get a new instance of String object.

To check this I wrote the following test code, and it indeed seems that the above block will work, but then there are other unexpected outcomes.

public class Test {

    public static void main(String[] args){

        new Thread() {
            public void run() {
                //f1("A", new X());
                f1("A", "Str");
            }
        }.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //f1("B", new X());
        f1("B", "Str");
    }

    public static void f1(String a, Object x) {
        synchronized(x) {
            System.out.println("f1: " + a);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1: " + a + " DONE");
        }
    }

    private static class X {
        public boolean equals(Object o) {
            System.out.println("equals called");
            return true;
        }

        public int hashCode() {
            System.out.println("hashCode called");
            return 0;
        }
    }

}

If you run the above code the you will get the following output:-

f1: A
f1: A DONE
f1: B
f1: B DONE

However, if I comment the f1("A", "Str"); and f1("B", "Str"); lines and uncomment the lines above them, then the outcome is:-

f1: A
f1: B
f1: A DONE
f1: B DONE

Since the Str version worked so I was expecting that maybe Java uses equals check for synchronized block or maybe hashCode but from second test it seems, it is not the case at all.

Is String a special case?

  • 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-10T09:44:06+00:00Added an answer on June 10, 2026 at 9:44 am

    No, Java does not use equals for lock monitors.

    The lock is on the object instance itself. So, in a way, it uses “==”, if you will (but in reality, this is not how it is implemented. Every object has a special slot for the current lock owner).

    There is no special case for String.

    What is happening with Strings, though, is that String literals get pooled, and if you have the same literal more than once, it will result in the same instance (whereas new X creates different instances, as would new String). If you call intern on your “new” Strings, you probably see the same effect.

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

Sidebar

Related Questions

We have couple different web-apps that share the same db link. The hibernate layer
I have couple of input fields with the class link. All of them should
I have couple of objects(1 custom object called appointment and event object) which i
Let's say we have very simple Java class MyClass . public class MyClass {
I have couple of forms with different ids but with same input names and
Possible Duplicate: Difference between object and instance I have couple of questions: Every instance
I have couple of controls in the page. I need to modify these value
I have couple of hyperlinks on my website. What I want is to remove
I have couple of dozen pieces of data that I need to save and
I have couple of formulas and data coming from database. I want to refresh

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.