HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

4) Multithreading Lesson

What is Java Multithreading?

8 min to complete · By Ryan Desmond

Just like humans, computers can do multiple things simultaneously (parallel programming), which is made possible thanks to threads.

Why use Java Threads?

Our computers/servers are very powerful. They are quite capable of handling more than one process at a time. They handle hundreds, if not thousands, of parallel processes at any given moment. Java was written to natively exploit this potential by supporting multithreaded programming, which improves the processing efficiency of your program

It is often the case that you have several tasks that need to be completed, but they only sometimes need to be executed sequentially. In this case, you can create new threads to handle these tasks that will run in parallel with the main Thread. (You have been using the main Thread since the first day of this course.) 

In the image below, you can see two use cases. The first shows a linear/sequential process where requirement "B" cannot begin until requirement "A" is completed, and requirement "C" cannot start until requirement "B" is completed.

Now imagine that requirements "A" and "C" are both collections of 4 simple tasks, each taking 1 second to complete, that do not need to execute in sequential order. In the first example, "A" and "C" will collectively each take 4 seconds to complete. If you're able to multi-thread these tasks in "A" and "C" and run them in parallel, you can collectively complete "A" and "C" in 1 second.

Flow chart showing a sequential flow from A, B to C, and a multithreaded version where A and C are split into four subtasks.

What Does Multithreaded Mean?

When you run any program, all code execution is done on a thread. By default, all the Java code you write runs on the main Thread. However, you can create many other threads that can run in parallel. When your code creates and uses multiple threads, it is called multithreaded.

Multithreading is akin to multitasking, and this helps to maximize the available processing power in your CPU. It is excellent for doing background tasks that the main Thread does not need to wait for. For instance, if you're cleaning the kitchen, you do not need to wait for the dishwasher to complete its cycle before wiping down the kitchen counters. By starting the dishwasher and letting it run on its own while you continue doing other tasks, you have started a parallel thread of execution.

That said, you can only start unloading the dishwasher once the cycle is complete. When the dishwasher is complete, it will ring out a sound, letting you know you can come and complete the dish task by unloading it. This same case plays out in multithreading, specifically, the notify() method, which will send out a signal to all interested threads, letting them know the process is complete. Anything that needs to happen next can now begin. We'll come back to the specifics of this.

The image below reiterates the number of processes you can complete when working with multithreaded/parallel programming.

Line diagram representing threads that are divided into four sections: two sequential and two parallel.

How to Create a Thread?

In Java, there are two ways to create a Thread:

  1. Implement the Runnable interface
  2. Extend the Thread class

The consensus is that if you want to create a generic Thread and start it, then it is simpler to implement the Runnable interface. This is also favorable as you can implement as many interfaces as needed, but you can only extend one other class. 

On the other hand, if you need to change the default behavior of the Thread class for any reason, then create your threads by extending the Thread class.

Both options for creating a Thread still use the underlying Thread class to instantiate, access, and control the underlying Thread. The only difference is how they are created. The Runnable interface is the easiest of the two. Here's an image illustrating the two options.

A flow chart showing the two options for creating threads in Java. Extending Thread and implementing Runnable.

Summary: What is a Thread in Java?

  • A Thread is a class in Java that represents a process of execution
  • Java is written to include multithreading
  • Multithreading is the process of executing multiple threads in parallel
  • Multithreading improves processing efficiency

How to Create a Thread?

  1. Implement the Runnable Interface
  2. Extend the Thread class