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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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!");
      }