Multithreading in Java – The Basics

This is one of the most trending as well as important topics in the interviews these days. Multi-threading, as the word implies, multiple threads, but basically, what are threads? Ok, here we go!

The Concept behind Multi-threading

Either opening an Excel or a Word document, or editing a file on Dropbox, the operating system starts a process, and begins executing the primary thread of that process. This primary thread might further create several other execution threads, with different tasks delegated to them, such that they do not step over one another during their lifetime. This means that, if I open a text file on my Dropbox, the primary thread might be opening it for me, which further creates another thread that accepts the user input and yet another thread to sync the new data to it’s servers. With this, we can now say that a single process, may or may not consist of multiple threads. The diagram below might make things more clear to you.


Now we are clear on what multithreading is, let’s note some of it’s features and advantages.

Features of Multithreading

  1. A thread can be supposed to be a single sub-process within the main process.
  2. Thread is the smallest unit of execution of a process.
  3. All the threads within a process share the same memory space allocated for that particular process.
  4. Threads are lightweight as we could think of, since it uses less resource as compared to a process.
  5. Multiple threads might be accessing the same resource at the same time, which should be strictly prohibited. Only one thread should be allowed to modify the shared resource at a particular instance. Therefore, multithreading requires careful programming.

Advantages of Multithreading

  1. Threads are independent of each other, so multiple threads can target their respective delegated tasks.
  2. Threads execute concurrently with a process, therefore speeds up the completion of the process and saves time.

How exactly can we create a thread in java?

So, now let’s see, by how many possible ways we can create a thread? Note that, this is one of the favorite questions of the interviewer.

There are two ways we can create a thread:

1. By extending a class with the Thread class,

2. By implementing a class with the Runnable interface,

As we learnt earlier, the primary thread of any process might start several other threads side-by-side, but remember, only as long as the OS allows it. Let’s see how this is possible with our code below:

Note that, whenever the JVM starts, it always creates the “main” thread for our application, that looks for the static void main(String[] args) method in the source code and invokes it. Assuming that this main thread is the primary thread of our sample process, note that it is starting two other threads one after the other, that might be serving two independent purpose as we discussed at the start of this article with an example of updating a dropbox file.

I hope we are now very much clear on the basic concept of Multithreading. We will plunge more deeper into the multithreading concepts in our upcoming articles. Thread communication and thread methods being the most important ones.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.