Another common scenario in multi-threaded programs is to wait for a thread to complete before proceeding in a path of execution.

In other languages, this concept is called “awaiting” or “blocking”. In Java, we say that we wait for the thread to join.

When a thread joins, it means that the thread’s task is complete and the process that was initially forked off has been “joined” back into the main thread.

This is useful when you can only perform a certain task once several prerequisite tasks have been completed. Take a look at BurgerMaker.java which is currently in the editor.

In this example, we can see how .join() is used to enforce dependencies between different tasks. By using .start() and .join() strategically, we can ensure that tasks that can be performed concurrently are started, but tasks that have dependencies are not started until all dependencies have been .joined().

In this exercise, we’ll use the .join() features we see in BurgerMaker.java to ensure that tasks occur in the right order while making a cake in our new file, CakeMaker.java.



Find the CakeMaker.java file in the editor and take a look at it. Several methods that detail the different steps in making a cake are already in place.

Notice that we’re using a different constructor for Thread now; one that takes two arguments. This one allows us to create the thread and give it a custom name in the same step.

Update the main function where specified, so that the tasks occur as efficiently and concurrently as possible, while not breaking dependencies between tasks.

The following constraints should apply:

  • The oven must be preheated before the cake can be baked.
  • The dry ingredients and wet ingredients should be mixed before the ingredients can be combined.
  • The ingredients must be combined before the cake can be baked.
  • The cake must be finished baking before the cake can be frosted.

Compile and run CakeMaker.java! You should see the cake-making process, and if you’ve done it correctly, several tasks will happen at once. Your cake will be ready in no time!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?