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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:05:28+00:00 2026-05-24T06:05:28+00:00

I have the following 2 classes code that produce this result for instance: Wainting

  • 0

I have the following 2 classes code that produce this result for instance:

Wainting for calculation to complete...
Calculator thread says HELLO!
T1 says that total is 10
Wainting for calculation to complete...
Wainting for calculation to complete...

Now threads are waiting but nobody is going to notify them.
How can I force the threads from T1 to T3 to run before the “Calculator thread” wake em up?

public class Calculator implements Runnable{

  private int total;

  public int getTotal() {
    return total;
  }

  @Override
  public void run() {
    synchronized (this) {

        for (int i = 0; i < 5; i++) {
            total += i;
        }
        System.out.println(Thread.currentThread().getName() + " says HELLO!");
        notifyAll();
    }
  }
}


import static java.lang.System.out;

public class Reader implements Runnable{

  private Calculator c;


  public Reader(Calculator calc) {
    c = calc;
  }

  public Calculator getCalculator() {
    return c;
  }

  public static void main(String[] args) {

    Calculator calc = new Calculator();
    Reader read = new Reader(calc);

    Thread thread1 = new Thread(read);
    Thread thread2 = new Thread(read);
    Thread thread3 = new Thread(read);

    thread1.setName("T1");
    thread2.setName("T2");
    thread3.setName("T3");

    thread1.start();
    thread2.start();
    thread3.start();

    Thread calcThread = new Thread(read.getCalculator());
    calcThread.setName("Calculator thread");
    calcThread.start();
  }
}


  @Override
  public void run() {
      synchronized (c) {
          try {
              out.println("Wainting for calculation to complete...");
              c.wait();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          out.println(Thread.currentThread().getName() + " says that " + "total is "  + c.getTotal());
      }

  }

}
  • 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-05-24T06:05:30+00:00Added an answer on May 24, 2026 at 6:05 am

    Thread.join() may seem like an option in this particular situation. Since you have control of the main() function and you know exactly when each thread is starting.

    A more general way to handle this situation is use a conditional variable and call the c.wait() within a loop to check the condition variable.

    Basically add the isFinished field in the Calculator class :

    public class Calculator implements Runnable {
    ...
      public volatile boolean isFinished = false
    ..
    ..
    

    Then you replace c.wait() with :

    ...
    while (!c.isFinished) {
      c.wait();
    }
    ...
    

    Finally in the `run() method of your Calculator class after calculating total, set the isFinished field

    ....
     for(int i = 0; ....
         total = += i;
     }
     c.isFinished = true
    ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes (I've trimmed the code): public class SqlWeightTrackerRepository : IWeightTrackerRepository
Ok, I have the following code that used to work but now it does
I have the following ColdFusion code that is getting information from a database and
Suppose that you have the following hierarchy of statistics-related classes, structured in a manner
I have following classes. class A { public: void fun(); } class B: public
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have the following classes: public class Person { public String FirstName { set;
I have the following classes in my models file class HardwareNode(models.Model): ip_address = models.CharField(max_length=15)
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>

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.