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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:44:32+00:00 2026-06-06T20:44:32+00:00

Earlier I posted a problem about implementing wait and notify, but I wasn’t very

  • 0

Earlier I posted a problem about implementing wait and notify, but I wasn’t very clear, so here is a more specific question.

In the long code block below there is one wait and one notify. The notify is supposed to stop the wait and cause it to stop waiting. At the moment, i think the wait works, but the notify does not. Could someone explain why the notify doesn’t notify the wait? Thanks!

Note: the rest of the code works i’m only interested in these two specific parts.

import com.fmr.ipgt.email.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import javax.mail.MessagingException;

class MyQuery {
synchronized void qQuery() throws Exception {
    String query = ".z.k"; // The query that is used to query q; this can be changed here.
    int version = 0;
    c qConn = null;
    qConn = new c(Main.host,Main.port); // Connect to the q database
      while (Main.healthy) {
          Object o = qConn.k(query); // Query q
          version = c.t(o);
          if(!(version==0)) {
              System.out.println(version);
              System.out.println("database healthy");
              NewThread.suspendFlag = false;
              notify(); 
              break; // End the process if the database responds
          }
          }

       System.out.println("reaches loop end");
  }
}

class MyThread implements Runnable {
  MyQuery myResource;

  MyThread(String name, MyQuery so) {
    myResource = so;
    new Thread(this, name).start();
  }

  public void run() {

    try {
      myResource.qQuery(); // Begin a method to query q.
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

class NewThread implements Runnable {
       String name; // name of thread
       Thread t;
       static boolean suspendFlag;
    private int minutes;
       NewThread(int minutes) {
           this.minutes = minutes;
           System.out.println("reaches constructor");
          t = new Thread(this);
          suspendFlag = true;
          t.start(); // Start the thread
       }
       // This is the entry point for thread.
       public void run() {
          try {
             synchronized(this) {
                while(suspendFlag) {

                   System.out.println("reaches wait");
                   wait(minutes*60000);

                   System.out.println("reaches end");
                   if(suspendFlag) {
                       Main.setHealth(false);
                    Main.sendMessages(); // The database has not responded for the given time. Report that it is unhealthy.
                   }
                   break;
                }
              }
          } catch (InterruptedException e) {
             System.out.println(name + " interrupted.");
          } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       }
    }

public class Main {

private static String[] recipients;
private static String subject = "Database Failure";
private static String message = "The database has failed or is in a hung state";
private static String from;
static String host;
static int port;
private static String emails;
private static int minutes;
static boolean healthy = true;
public static void main(String args[]) throws Exception {

    // Import information from the configuration file
      SAXBuilder builder = new SAXBuilder();
      File xmlFile = new File("/export/home/rhadm/file.xml"); // Note: The directory for the configuration file may need to be changed

      try {

        Document document = (Document) builder.build(xmlFile);
        Element rootNode = document.getRootElement();
        List list = rootNode.getChildren("parameters");
           Element node = (Element) list.get(0);

          host = node.getChildText("host");
           port = Integer.parseInt(node.getChildText("port"));
           emails = node.getChildText("emails");
           String delims = "[ ]+";
           recipients = emails.split(delims); // parse email list
           minutes = Integer.parseInt(node.getChildText("time"));
           from = node.getChildText("from");

      } catch (IOException io) {
        System.out.println(io.getMessage());
      } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
      }
    MyQuery unhealthy = new MyQuery();
    NewThread ob1 = new NewThread(minutes);
    new MyThread("MyThread", unhealthy);  // Create new Thread


  }
  public static void setHealth(boolean health){
      System.out.println("database unhealthy");
    healthy  = health;  
  }

  public static void sendMessages() throws MessagingException {
        System.out.println("sending emails");
      FCAPMailSender.postMail(recipients,subject,message,from);
  }
  }
  • 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-06T20:44:34+00:00Added an answer on June 6, 2026 at 8:44 pm

    You are synchronizing on different objects. The notify will only effect objects synchronized-waiting on the same object & instance.

    The waiting thread is synchronized & waiting on a NewThread while the notifying thread is doing so on a MyQuery instance

    Have a shared object.

    private final Object LOCK = new Object();
    synchronized(LOCK){
      LOCK.wait();
    }
    synchronized(LOCK){
      LOCK.notify();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted a question about this earlier, but I have more information now and
This simple problem is kiling me. I posted something earlier about trying to clean
I posted earlier today about template classes, but was pretty far off and got
I had earlier posted a question, regarding same, but over here i want guidance
I posted earlier with a problem regarding the same program but received no answers.
I posted earlier (and received valid answers, thanks) about a problem I had loading
I posted a question earlier about why django redirect was not working correctly, but
I had posted another question earlier about deployment with Passenger. That problem turned out
I posted earlier about getting my VBScript to wait until a process had finished
I've posted here earlier about how to make a tic tac toe for two

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.