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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:52:50+00:00 2026-06-14T22:52:50+00:00

Consider the following: public class Deadlock { static class Friend{ private final String name;

  • 0

Consider the following:

public class Deadlock {
static class Friend{
    private final String name;

    public Friend (String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }


    public synchronized void bow(Friend bower){
        System.out.format("\n%S: %S has bowed to me!" , this.name, bower.getName());
        bower.bowBack(this);
    }
    public synchronized void bowBack(Friend bower) {
        System.out.format("\n%S: %S has bowed back to me!", this.name, bower.getName());
    }


}

public static void main(String[] args) {
    final Friend alf = new Friend("Alf");
    final Friend arian = new Friend("Arian");

    // Thread 1
             new Thread(new Runnable()  {
        public void run(){ alf.bow(arian);}}).start();

    //Thread 2
    new Thread(new Runnable()  {
        public void run(){ arian.bow(alf);}}).start();  
}

}

The out put is

ALF: ARIAN has bowed to me!
ARIAN: ALF has bowed to me!

LOCK situation…..

When Thread 1 runs, it requires a lock on the object Friend. Immediately after that Thread 2 requires lock on the second object. Now the method bow is lock by thread 1 and thus prints “ALF: ARIAN has bowed to me!”. How comes Thread 2 enters bow and neither can enter * bowBack * ??

Regards B.

  • 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-14T22:52:51+00:00Added an answer on June 14, 2026 at 10:52 pm

    Bear in mind that synchronized methods will always synchronise on this.

    There are two objects and therefore two locks. alf.bow() acquires alfs lock and arian.bow() acquires arians. But then bowback() tries to acquire the other one, which is when the deadlock happens.

    If you want to synchronise on a shared lock object, you should do something like this:

    class Friend {
       private static final Object lock = new Object();
    
       public void bow(...) {
         synchronized( lock ) {
            ...
         }
       }
    
       public void bowback(...) {
         synchronized( lock ) {
            ...
         }
       }
    
    }
    

    Since lock is in a static field, it means all instances of Friend use the same lock object, therefore there’s no chance of a deadlock.

    You can also synchronise on the class object synchronized( Friend.class ) {...}, but synchronising on externally visible objects usually isn’t a good idea as there’s no guarantee that some other part of the code doesn’t steal the lock.

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

Sidebar

Related Questions

Consider the following code: public class Foo { private static final Object LOCK =
Consider following code public class City { public string Name { get { return
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following class hierarchy: public class Foo { public string Name { get;
Consider the following snippet: public class ReflectionTest { public static void main(String[] args) {
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
Consider the following class public class PlanetKrypton { public static void CallSuperManforHelp(string helpMessage, params
Original Question Consider the following scenario: public abstract class Foo { public string Name
Consider the following class: public class Score { private static readonly Guid _relationId =
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries

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.