Basics of Multithreading in Java

Dan Dias Abeyesinghe
Javarevisited
Published in
4 min readJan 26, 2022

--

Photo by Luca Bravo on Unsplash

Basics in creating Threads in Java

What is multi-threading? In simple terms multithreading is the process of parallel execution of two or more parts of a program with the intention of maximum utilization of the CPU.

Nowadays a computer can perform more than one task at a time and that is thanks to the multithreading concept. There are two ways this process can be handled by the computer, one is using a single processor where time shifting is used, and the other is using multiple processors where multiple tasks are run.

There are two types of multi-tasking,

· Process based

· Thread based

Process based multitasking is something like you could listen to music using your music player, working on a project using VS code and at the same time use your browser to surf the internet.

Thread based multitasking could be something like you could surf through the internet while downloading some files from the internet at the same time. Here surfing and downloading are done on two different threads at the same time.

Types of threads

As we all know the Java virtual machine consists of number of threads. It mainly consists of Application threads and system threads.

System threads can be broken down into Garbage Collector thread, Virtual Machine thread and complier thread.

However, the developer is allowed to create many threads as they wish in the application but that also depends on the type of JVM used.

Creating a Thread

There are two ways of creating a thread.

1. By extending the Thread class

2. By implementing the Runnable interface

By extending the Thread class

Here what we do is we create a class where it extends the Thread class and the run() method in the Thread class is overridden by the class created. Next we create a new object of the class we created and call the start() method to start the execution of the Thread. The run() method is what is executed when the start() method is called.

Extending the Thread class

Here as it is clear from the above code snippet, a class named ‘MyThread’ extends the Thread class and overrides the run()method. Then a new object called ‘myThreadObject’ is created and the start()method is called on that object resulting in invoking the run() method on the Thread object

By implementing the runnable interface

In this approach we create a class that implements the Runnable interface. The class overrides the run() method and we create a new thread object and call the start() method on that object.

It is important to note that the Runnable interface has only one single method called run().

Implementation the Runnable Interface

As show in the above code, the class ‘MyRunnable’ implements the Runnable class and it overrides the run() method. Then a new instance of the MyRunnable class is created and is passed into the Thread constructor as show above. Finally the start() method is classed on the Thread object.

When the thread is started by calling the start() method the MyRunnable run() method will be executed as it overridden the run() method in the Runnable interface.

What we need to consider when choosing one of these ways of implementing a thread.

· It’s always a good idea to use the Runnable interface to start a new thread. Otherwise, the thread class will lose the inheritance feature.

· If we extend the Thread class in our project, we cannot extend any other classes as we all know java does not support multiple inheritance. therefore, we could implement the Runnable interface which will allow us to extend other classes.

Important:

When creating and starting a thread a common mistake many do is that they call the run() method instead of the start() method. This however will execute the code given inside the run() method but a new thread will be not be created and it will continue on the current thread where the previous lines of code were executed. Because when a program invokes the start() method, it creates a new thread and then calls the run() method.

However, if we call the run() method directly, no new thread is created and the run() method is executed as a normal method on the current thread with no multi-threading.

I hope this short article on the brief introduction to Basics of Multithreading helped you to get a better and a clear understanding of the discussed topic. Please do feel free to provide feedback on the above and do follow for more articles related to this and many more!

--

--