Skip to main content

Threads in Java - Basics (Part 1)

Hello everyone, today we are going to discuss one of the most important concepts in Java - Multithreading.
In the first part of this multi-part blog series, we will be discussing the basics of threads, how can we create them? which is the best approach? and many other questions like these. So without further ado, let's get started.

What are threads?

Let's say you are Batman (pretty cool, eh? 😊) and your arch-enemy, the master alchemist Ra's al Ghul has brewed a dangerous potion from the Lazarus Pit. To avenge his earlier defeats by the hands of The Dark Knight, he has spread the potions all over the Gotham city. 
Now, the only way to save citizens of Gotham is to find the cure of the potion (hidden in the city somewhere) and find Ra's as well before he can devise a new plan to destroy the city. 
Batman calls Nightwing (the original Robin) and asks his help in finding Ra's. Batman decides to find the cure of the deadly potion.
In the end, both Batman and Robin were able to locate the cure and Ra's successfully and saved the day.

What do we understand from this story? Look how Batman divided the tasks between himself and Robin. This made them do two tasks simultaneously and in the half amount of time. This is what threads do - they perform tasks concurrently.

Multithreading allows executing different parts of a program to achieve the maximum CPU utilization. Few properties of threads are -
  1. We can do multiple activities within a single process.
  2. Light-weight process.
  3. A thread shares the memory with other threads.
  4. But each thread has a separate program counter, stack and local variables.
When a thread is invoked, there are two paths of execution - thread's own path and the path of the program which created the thread.

Creating a thread

We can create threads in two ways
  • Extending the Thread class
  • Implementing the Runnable interface
Below code shows both the ways of creating a thread - 

As you can see that in the first approach we are extending the Thread class and overriding the run() method. While in the second approach we are implementing the Runnable interface and implementing the run() method.
Then from our main class, we are calling the start() method to invoke the threads. Behind the scenes, the start() method calls the run() method.

After running the code, we will see the following output

Thread created by implementing Runnable interface 14 is running
Thread created by extending Thread class 13 is running
Thread created by extending Thread class 15 is running
Thread created by implementing Runnable interface 16 is running
Thread created by extending Thread class 17 is running
Thread created by implementing Runnable interface 18 is running

Thread class vs Runnable interface

  • The basic difference between Thread and Runnable is that each thread defined by extending the Thread class creates a unique object and get associated with that object. On the other hand, each thread defined by implementing Runnable interface shares the same object.
  • Since a unique object is created while extending the thread class, hence more memory is required. But if we implement the Runnable interface, this will not happen.
  • If we extend the Thread class then we cannot extend any other class as Java does not support multiple inheritance. But if we implement the Runnable interface then we can extend other class as well as implement other interfaces.
  • We should extend Thread class only if we have to override or specialise some other methods of Thread class. We must implement a Runnable interface if we only want to specialise run method only.
  • Extending the Thread class introduces tight coupling in the code as the code of Thread and job of the thread is contained by the same class. On the other hand, Implementing Runnable interface introduces loose coupling in the code as the code of Thread is separate from the job assigned to the thread.


In this post, we discussed the basics of threads, how can we create them and the differences between Thread and the Runnable class. The code is found in the commit on my GitHub.

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😊 and Happy Learning 😊


  1. This comment has been removed by the author.

  2. Hi , Please explain the last difference b/w thread and runnable related to coupling with a more detailed example. Thanks

  3. Casino Bonus Codes - December 2021
    No deposit bonus casino promotions. We recommend goyangfc 2021 casino bonus codes and promos for new players. We also list new casino bonuses for December 2021.


Post a Comment

Popular posts from this blog

Parsing XML using Retrofit

Developing our own type-safe HTTP library to interface with a REST API can be a real pain as we have to handle many aspects - making connections caching retrying failed requests threading response parsing error handling, and more.  Retrofit, on the other hand, is a well-planned, documented and tested library that will save you a lot of precious time and headaches. In this tutorial, we are going to discuss how we can parse the XML response returned from  using the Retrofit library. To work with Retrofit, we need three classes -  Model class to map the JSON data Interfaces which defines the possible HTTP operations Retrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL endpoint for the HTTP operation. Every method of the above interface represents on possible API call. The request type is specified by using appropriate annotations (GET, POST). The respon

Threads in Java - CountDownLatch (Part 12)

A CountDownLatch is a synchronizer which allows one thread to wait for one or more threads before starts processing. A good application of  CountDownLatch is in Java server-side applications where a thread cannot start execution before all the required services are started. Working A  CountDownLatch is initialized with a given count which is the number of threads it should wait for. This count is decremented by calling countDown() method by the threads once they are finished execution. As soon as the count reaches to zero, the waiting task starts running. Code Example Let us say we require three services, LoginService, DatabaseService and CloudService to be started and ready before the application can start handling requests. Output Cloud Service is up! Login Service is up! Database Service is up! All services are up. Now the waiting thread can start execution. Here, we can see that the main thread is waiting for all the three services to start before starting its own

Threads in Java - Masterclass (Part 0)

Threads in Java Multithreading is a way to introduce concurrency in a program. In any case, if there are parallel paths in our program (parts which do not depend on the result from another part), we can make use of multithreading. One should exploit this feature, especially with all these multiple core machines nowadays. Below are a few reasons why we should use multithreading - 1. Keep a process responsive There was once a time when you would print a document in MS Word and the application would freeze for an annoyingly long amount of time until the job finished. Eventually, Microsoft solved this problem by running a printing job parallel to the main thread/ GUI thread.  To be clear though, not only GUI apps but Network services have to keep an ear to the ground for new clients, dropped connections and cancellation requests. In either case, it is critical to do the heavy lifting on a secondary thread to keep the user satisfied. 2. Keep a processor busy Keeping a proc