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.

Conclusion

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 😊


Comments

Popular posts from this blog

Threads in Java - Masterclass (Part 0)

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 processor busy can be a tough task e…

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 connectionscachingretrying failed requeststhreadingresponse parsingerror 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 https://timesofindia.indiatimes.com/rssfeedstopstories.cms using the Retrofit library.

To work with Retrofit, we need three classes -  Model class to map the JSON dataInterfaces which defines the possible HTTP operationsRetrofit.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 response is returned as a Call object…