Java Multi-Threading

Multithreading in java is a process of executing multiple threads simultaneously.

Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

But we use multithreading than multiprocessing because threads share a common memory area. They don’t allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.

Java Multithreading is mostly used in games, animation etc.

Creating and Starting Threads

Creating a thread in Java is done as below:

Thread thread = new Thread();

To start the Java thread you will call its start() method, like this:


This example doesn’t specify any code for the thread to execute. The thread will stop again right away after it is started.

There are two ways to specify what code the thread should execute.

  1. The first is to create a subclass of Thread and override the run() method.
  2. The second method is to pass an object that implements Runnable (java.lang.Runnable to the Thread constructor.

Thread Subclass

The first way to specify what code a thread is to run, is to create a subclass of Thread and override the run() method. The run() method is what is executed by the thread after you call start()

public class MyThread extends Thread {

public void run(){
System.out.println(“MyThread is running”);

To create and start the above thread you can do like this:

MyThread myThread = new MyThread();

The start() call will return as soon as the thread is started. It will not wait until the run() method is done. The run() method will execute as if executed by a different CPU. When the run() method executes it will print out the text “MyThread is running”.

You can also create an anonymous subclass of Thread like this:

Thread thread = new Thread(){
public void run(){
System.out.println(“Thread is Running”);

This example will print out the text “Thread running” once the run() method is executed by the new thread.

Runnable Interface Implementation

The second way to specify what code a thread should run is by creating a class that implements java.lang.Runnable interface. The Runnable object can be executed by a Thread.

Java Runnable example:

public class MyRunnable implements Runnable {

public void run(){
System.out.println(“MyRunnable running”);
To have the run() method executed by a thread, pass an instance of MyRunnable to a Thread in its constructor. Here is how that is done:

Thread thread = new Thread(new MyRunnable());
When the thread is started it will call the run() method of the MyRunnable instance instead of executing it’s own run() method. The above example would print out the text “MyRunnable running”.

You can also create an anonymous implementation of Runnable, like this:

Runnable myRunnable = new Runnable(){

public void run(){
System.out.println(“Runnable running”);

Thread thread = new Thread(myRunnable);

Thread vs Runnable in Java

1) Java doesn’t support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java.

2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead.

3) Runnable interface represent a Task which can be executed by either plain Thread or Executors or any other means. so logical separation of Task as Runnable than Thread is good design decision.

4) Separating task as Runnable means we can reuse the task and also has liberty to execute it from different means. since you can not restart a Thread once it completes.

5) Java designer recognizes this and that’s why Executors accept Runnable as Task and they have worker thread which executes those task.

6) Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable.



Comments are closed