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

  • Home
  • SEARCH
  • 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 9149141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:26:12+00:00 2026-06-17T11:26:12+00:00

THIS IS NOT A DUPLICATE OF Java, Multithreading with synchronized methods … he didn’t

  • 0

THIS IS NOT A DUPLICATE OF Java, Multithreading with synchronized methods… he didn’t ask the same question.

I am trying to write a program that using multi-threading and deadlocks. I am having trouble understanding how and where to use wait() and notify() in my synchronized methods to have the thread wait and wake it up with notify(). I have it to make the first thread wait after the ping statement so the next thread will start it’s ping statement too. But I have a notify() to wake up the first thread and I can’t figure out why the thread will not wake up. The following is suppose to be my output:

Starting...1
Girl (ping): pinging Boy
Boy (ping): pinging Girl
Girl (ping): asking Boy to confirm
Boy (confirm): confirm to Girl
Girl (ping): got confirmation
Boy (ping): asking Girl to confirm
Girl (confirm): confirm to Boy
Boy (ping): got confirmation

My code is deadlocking or I am not using wait and notify correctly because it stops here:

Starting...1
Girl (ping): pinging Boy
Boy (ping): pinging Girl
Girl (ping): asking Boy to confirm
Boy (confirm): confirm to Girl
Girl (ping): got confirmation

Here is my code:

Monitor.java

class Monitor {
String name;

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

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

   public synchronized void ping (Monitor p) {
      p.release(p);
      System.out.println(this.name + " (ping): pinging " + p.getName());
      notify();
      try { wait(); } catch (Exception e) {}
      System.out.println(this.name + " (ping): asking " + p.getName() + " to confirm");
      p.confirm(p);
      System.out.println(this.name + " (ping): got confirmation");
   }

   public synchronized void confirm (Monitor p) {
      System.out.println(this.name + " (confirm): confirm to " + p.getName());
   }

   public synchronized void release (Monitor p) {
      notify();
   }
}

Runner.java

public class Runner extends Thread {
Monitor m1, m2;

    public Runner (Monitor m1, Monitor m2) { 
      this.m1 = m1; 
      this.m2 = m2; 
    }

    public void run () {  
       m1.ping(m2);  
    }
}

Deadlock.java

public class DeadLock {
   public static void main (String args[]) {
      int i=1;
      System.out.println("Starting..."+(i++));
      Monitor a = new Monitor("Girl");
      Monitor b = new Monitor("Boy");
      (new Runner(a, b)).start();
      (new Runner(b, a)).start();
   }
}
  • 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-17T11:26:13+00:00Added an answer on June 17, 2026 at 11:26 am

    You are calling:

    (new Runner(a, b)).start();
    // which effectively calls in run()
    a.ping(b);
    

    This locks a calls notify on a and then waits on a.

    The other thread calls:

    (new Runner(b, a)).start();
    // which effectively calls in run()
    b.ping(a);
    

    This locks b calls notify on b and then waits on b.

    Since neither of the threads is notifying the other one, they will never leave that state. I’m not sure what you are trying to accomplish so it’s hard to figure out how to fix the code.

    Edit:

    Now that you’ve edited your code you have a more standard deadlock.

    • The first thread locks a then tries to lock b to notify it.
    • The second thread locks b then tries to lock a to notify it.

    Each thread has their lock but needs the other lock to continue. Classic deadlock.

    I suggest that you learn to use the debugger @OhioState22. Set break points and step through your code. Here’s a decent Eclipse tutorial.

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

Sidebar

Related Questions

I hope this question is not a duplicate but I didn't find anything equivalent
This is NOT a duplicate question, and the problem is driving me crazy. I
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
Possible Duplicate: Java += operator In Java, this is not valid (doesn't compile), as
Duplicate of this question . update - This is not an exact duplicate. See
I saw this question and some similar and I think it's not duplicate :
Possible Duplicate: Why this is not compiling in Java? In java, curly braces are
Possible Duplicate: Why is this statement not working in java x ^= y ^=
Possible Duplicate: Java Too Many Open Files This is not a duplicate, the referred
This is not a duplicate post, i just done research but not helping. First,

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.