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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:04:14+00:00 2026-06-12T14:04:14+00:00

I have some trouble understanding the concept of deadlock situation in this program. I

  • 0

I have some trouble understanding the concept of deadlock situation in this program.
I am getting the output as :
Entered amethod
Entered bmethod
and then the deadlock situation occurs.
Now since my amethod is a synchronized method, shouldn’t it execute first completely i.e. by calling bsum method and then start the new thread. ?
Please explain…

public class Deadlock 
{
    public static void main(String[] args) 
    {

        A a= new A();
        B b= new B();
        new MainClass1(a,b);
        new MainClass2(a,b);
    }

}
class MainClass1 extends Thread
{
    A a;
    B b;
    MainClass1(A a,B b)
    {
        super();
        this.a=a;
        this.b=b;
        start();
    }
    public void run()
    {
        a.amethod(b);
    }
}
class MainClass2 extends Thread
{
    A a;
    B b;
    MainClass2(A a,B b)
    {
        super();
        this.a=a;
        this.b=b;
        start();
    }
    public void run()
    {
        b.bmethod(a);
    }

}
class A
{
    public synchronized void amethod(B b)
    {
        System.out.println("Entered amethod");
        try{
            Thread.sleep(500);
        }catch(Exception e){}
        b.bsum(2,3);
    }
    public synchronized void asum(int a,int b)
    {
        System.out.println("Sum in A is");
        System.out.println(a+b);
    }
}
class B
{
    public synchronized void bmethod(A a)
    {
        System.out.println("Entered bmethod");
        try{
            Thread.sleep(500);
        }catch(Exception e){}
        a.asum(3, 5);
    }
    public synchronized void bsum(int a, int b)
    {
        System.out.println("Sum in B is");
        System.out.println(a+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-12T14:04:16+00:00Added an answer on June 12, 2026 at 2:04 pm

    actually you have started both the threads… lets call the threads 1 and 2

    so what happens is when thread 1 acquired the lock over object A and calls the method amethod at the same time
    thread 2 has acquired the lock over object B and called the bmethod.
    now A wants to call the b’s sum method that is locked because B has already the lock over the object B.
    and B wants to call the sum method of A where A has already got the object of A and its not releasing the lock until its done with the calling of sum method.

    just remove the synchronized keyword from the sum methods and it will work( i mean not go into deadlock condition)

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

Sidebar

Related Questions

I have some trouble understanding the output from the following code: int main(void) {
I have some trouble understanding this one so here it is. I'm trying to
I'm having some trouble understanding this issue. I have a class: class StringProperty {
I have some trouble understanding the need for std::result_of in C++0x. If I understood
I am having some trouble understanding this brute force approach. I am using Perl
I'm new to CMake and have trouble understanding some usage concepts. I'm calling a
I'm having some trouble understanding how codeigniters loading works. Well first you have the
I have trouble understanding a fundamental concept in Java 2D. To give a specific
I have some trouble understanding parsing XML structures with SAX. Let's say there is
I'm having some trouble understanding import This piece of code works properly. I from

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.