FAQ: Threading: Lesson - Communicating Between Threads

This community-built FAQ covers the “Communicating Between Threads” exercise from the lesson “Threading: Lesson”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Java

FAQs on the exercise Communicating Between Threads

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

What is this for in the lesson’s example? I thought it has something to do with the synchronized block, but the task’s solution doesn’t have it. Codecademy taught me only two usages of the this keyword: referring to class variables in a constructor method if constructor’s parameters share their names with them; and calling a non-static method from inside of another non-static method with the same object that called that “outer” method in the first place. This has nothing to do with any of those cases

while (!this.foodArrived) {
         printTask("Waiting for the food to arrive...");
         wait();
       }

Once it is out of the while loop (because the mixing bowl is no longer in use), it should use printTask to say: "Using mixing bowl!" , before setting mixingBowlInUse to true to indicate that the resource has been claimed.

In the solution, on the other hand, they do it after setting the boolean to true. If it doesn’t matter, they shouldn’t’ve specified it. Besides, I suspect it’s why I can’t get the green checkbox here and continue the course

UPD: Can you believe it? They didn’t accept my code simply because of extra spacing! I kid you not. I just deleted two spaces (nothing else), pressed Run again, and it passed!

Why do they put a semicolon after this synchronized block

but don’t put it after this one

?

Do I need a separate synchronized(this) block for each boolean (one for the bowl, another for the whisk)?

Also…

I added synchronized(this) blocks for the whisk, but the code freezed at some point. What did I do wrong?

public void mixDryIngredients() {
    try {
      printTask("Mixing dry ingredients...");
      synchronized(this) {
        while(mixingBowlInUse) {
          printTask("Waiting for the mixing bowl...");
          wait();
        }
        mixingBowlInUse = true;
        printTask("Using mixing bowl!");
      }
      Thread.sleep(200);
      printTask("Adding cake flour");
      Thread.sleep(200);
      printTask("Adding salt");
      Thread.sleep(200);
      printTask("Adding baking powder");
      Thread.sleep(200);
      printTask("Adding baking soda");
      Thread.sleep(200);
      synchronized(this) {
        while(whiskInUse) {
          printTask("Waiting for the whisk...");
          wait();
        }
        whiskInUse = true;
        printTask("Using the whisk!");
      }
      printTask("Mixing...");
      Thread.sleep(200);
      synchronized(this) {
        printTask("Releasing the whisk!");
        whiskInUse = false;
        notifyAll();
      }
      synchronized(this) {
        printTask("Releasing mixing bowl!");
        mixingBowlInUse = false;
        notifyAll();
      }
      printTask("Done!");
    } catch (InterruptedException e) {
      System.out.println(e);
    }
  };

  public void mixWetIngredients() {
    try {
      printTask("Mixing wet ingredients...");
      synchronized(this) {
        while(mixingBowlInUse) {
          printTask("Waiting for mixing bowl...");
          wait();
        }
        printTask("Using mixing bowl!");
        mixingBowlInUse = true;
      };
      Thread.sleep(1000);
      printTask("Adding butter...");
      Thread.sleep(500);
      printTask("Adding eggs...");
      Thread.sleep(500);
      printTask("Adding vanilla extract...");
      Thread.sleep(500);
      printTask("Adding buttermilk...");
      Thread.sleep(500);
      synchronized(this) {
        while(whiskInUse) {
          printTask("Waiting for the whisk...");
          wait();
        }
        whiskInUse = true;
        printTask("Using the whisk!");
      }
      printTask("Mixing...");
      Thread.sleep(1500);
      synchronized(this) {
        printTask("Releasing the whisk!");
        whiskInUse = false;
        notifyAll();
      }
      synchronized(this) {
        printTask("Releasing mixing bowl!");
        mixingBowlInUse = false;
        notifyAll();
      }
      printTask("Done!");
    } catch (InterruptedException e) {
      System.out.println(e);
    }
  };

UPD: I did it! The key was to make the makeFrosting thread begin the process only if both the whisk and the bowl are available. Otherwise, it led to a comical situation when makeFrosting takes the whisk but does nothing because mixWetIngredients has the bowl while mixWetIngredients mixes the ingredients but then stops since it doesn’t have the whisk :laughing:

synchronized(this) {
        while(whiskInUse || mixingBowlInUse) {
          printTask("Waiting for the whisk and the bowl...");
          wait();
        }
        whiskInUse = true;
        mixingBowlInUse = true;
        printTask("Using the whisk and the bowl!");
      }

I have been quite disappointed with Intermediate Java course in general. I started this straight after Learn Java and was hoping same quality. Lots of errors in compilation for example in this CakeMaker class- How can this be fixed to see how the program behaves?

Anytime you see this it refers to the object that instantiated the class. For example.

class someClass {
  int someNum;
 
  public someClass (int num) {
    this.someNum = num;
  }
  public int getNum() {
    return this.someNum;
  }

  public static void main(String[] args) {
    someClass a = new someClass(6);
    
    System.out.println(a.someNum);
    System.out.println(a.getNum());
 }
}

Both statements in main end up calling a.sumNum. What happens when you use the getter method is the object a calls the class method getNum() which calls this.sumNum which will get replaced by the instance of the object calling it which in this case is a. Hope that helped.

Yeah this course is some hot garbage for sure. You just kind of have to fight through these things yourself on here. They have all kinds of projects and things broken all over the place and if it isn’t something super easy there is no help for you.

I would suggest if you got all the checkmarks up to the point where it is asking you to compile and it is giving you that, you reset the project and tried it again and got the same thing, and also understand the underlying concepts just hit give up and move onto the next thing. Just FYI these things they are having us do are not good examples of clean code or how you should do things in general anyhow. Learn the underlying concepts and don’t waste time on poor instructions and the broken code you will run into.