Java’s Thread model is outdated

Adil Wadhwania
5 min readJun 3, 2023

--

In my last article I have explained what is multi-threading and how it works, now in this article we will see how to create a Thread in java.

Multithreading in java

Thread in Java

  • There are two ways to create a thread in java, one is by extending the Thread class and the another is by implementing the Runnable interface.

1) Create a Thread by extending Thread Class

  • Let us first see how to create a Thread by extending the “Thread class”.

/**
* Creating a thread by extending the Thread class in java
*/
public class UsingThreadClass extends Thread
{
/**
* We need to implement the run method because whenever you call
* start method on the Thread object it internally calls the thread's
* (ThreadImpl) run method that is implemented here
*/
@Override
public void run()
{
System.out.println("Running a thread by extending the Thread class");
}
}

public class Main
{
public static void main(String[] args)
{
UsingThreadClass targetObj=new UsingThreadClass();
Thread t1=new Thread(targetObj,"Thread A");
t1.start();// called the start method of the thread
}
}
  • As we all know the execution of java program starts from main method so we will start from there and then understand about the “Thread execution class”.
  • First we have create an object of “UsingThreadClass” class that extends the “Thread” class, that means this class is the thread execution class and has a “run” method in it, which is executed when the “start” method of thread is invoked.
  • So each and every class that extends “Thread” class will have implementation of “run” method, because this “run” method is from where the threads execution starts when “start” method of Thread is invoked.
  • Then next thing we have done is created an object of “Thread” class and passed two things in the constructor of that object, first one is the target object this simply means which class “run” method to execute when the “start” method is called and second parameter is string that is the name of the thread.
  • We have passed “targetObj” variable in the first parameter of “Thread’s” constructor that means the run method of “UsingThreadClass” will be executed when the “start” method is called and in seconfd parameter we have passed the name of thread as “Thread A”.
  • The last thing we have done in the main method is called the start method of “Thread t1”, so now it will execute the run method of “UsingThreadClass” class which was passed as target object in the Thread.
  • In run method we have a simple print statement that prints, ”Running a thread by extending the Thread class”, you can write your own implementation in the run method whatever you want, if you want to call another run method then you can do that also.
  • It is not necessary to pass the name of thread in the constructor of “Thread” class you also create an object of “Thread” as given below.
// creating a thread by passing the target object only in constructor
Thread t1=new Thread(target);
  • This is how you can create a single thread in java, by creating the multiple “Thread” object and executing it you can create multiple threads but only few of them will be executed according to system’s scheduler scheduling the thread. Read What is Multithreading for more details.
  • We have another way to execute the Thread and that can be done as shown in the code below.
// this will internally call the run method of the "UsingThreadclass" class
targetObj.start();

2) Create a Thread using Runnable Interface

  • Let us understand why there are two ways to create a thread in java and which one to use.
  • Java does not support the multiple inheritance due to diamond problem, so by extending the “Thread” class to create a Thread, you won’t be able to extend any other class.
  • If you don’t want to extend a class to any other class than the “Thread” class then create the Thread execution class by extending “Thread” class otherwise use the “Runnable” interface because in java we can implement multiple interface but can not extend multiple classes.

/**
* Creating a thread by implementing the runnable interface
*/
public class UsingRunnable implements Runnable
{
/**
* We need to implement the run method because this class has
* implemented the Runnable interface and this interface has only
* one abstract method that is "run" method.
*/
@Override
public void run()
{
System.out.println("Running the thread by implementing
Runnable interface");
}
}

public class Main
{
public static void main(String[] args)
{
UsingRunnable targetObj=new UsingRunnable();
Thread t1=new Thread(targetObj,"Thread B");
t1.start();// will internally call run method of UsingRunnable class
}
}
  • By implementing the “Runnable” interface also we can make a class the Thread Execution class in java, it means we can create the threads by passing the object of a class that implements “Runnable”.
  • Runnable interface is a functional interface, that means it has only one method, that is “run ”method and we have implemented that in the “UsingRunnable” class.
  • In the main method when we pass the object of “UsingRunnable” class in the Thread t1 object and call the start method then internally the run method of the “UsingRunnable” class is executed and we get output as “Running the thread by implementing Runnable interface”.
  • In the Thread t1 object we have also passed one more thing and that is the name of the Thread, it is not compulsory to pass as discussed earlier.
  • This are the two ways to create a thread in java, first by extending the “Thread” class and another one is using “Runnable” interface.
  • In this article I have created only one thread by passing respective target object but you can created more than one thread and call start method on that threads.
  • In java if you don’t create any thread then also we have a thread that is main thread that executes the java main method. And you often see about the main thread in the exception like
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.java.concurrency.example.producer.consumer.X.main(ProducerConsumer.java:10)

“Thank you for taking the time to read my article! I hope you found the information and insights shared here helpful and interesting. If you enjoyed this article, be sure to follow my article for more similar content. I’m always exploring new topics and sharing my thoughts and insights on a variety of subjects. I appreciate your support and look forward to connecting with you through my writing. Until next time!”

ADIL WADHWANIA :)

Topics :- Threading in java, Threads in java, Thread, Multithreading, Create a Thread in java, Thread class in java, Runnable interface in java.

--

--

Adil Wadhwania
Adil Wadhwania

Written by Adil Wadhwania

Tech-savvy writer sharing insights on programming,current affairs,sports,great personalities, and new technologies. Follow for engaging and informative content.