Nuances of Instantiating and Starting a Thread

instantiating a thread in java

Multi-threading is not a simple task to achieve but if you remember the basics, it might be the easiest task for you. In the last post Java Threads – Do you know the basics?, we discussed the difference between two ways of defining a thread. We will now take the same topic forward by adding more about instantiating  and starting a thread. But you have to ensure that you have understood everything in previous post – Java Threads – Do you know the basics?. If not you can read it again and ask us question.

Thread class implements the Runnable interface. Runnable interface and Thread class both can be used to for defining a new thread. Implementing Runnable interface gives a job instance and providing same instance to Thread class’s constructor creates a new thread(a job runner or worker). While extending a Thread class and then instantiating it itself gives a runner with job. But basically it’s all about creating some parallel work units which might do same or different jobs.


Run Method – Thread’s program stays here

Runnable interface gives us only one abstract method run(), which needs to be defined in the class which is implementing the Runnable interface. Thread class does that. It implements the Runnable interface and defines run() method. If you select implementing Runnable interface way of defining a thread, then you have to implement the run() method in your class, but if you go with extending Thread  class way of defining a thread then having a run method in your class is optional, but if you won’t have it you are actually not doing anything in new thread 😛 .

Following is an example of Multi-threading. We have created three classes in this example. One for the main() method which creates two more threads. Other two classes are for threading. Class ThreadBike extends Thread class, which will be initialized and then start its thread. But the Class ThreadCar requires to pass its instance to other Thread class’s constructor, and then start its thread. Just go through the code and you will get the gist.

public class ThreadBike extends Thread {
       //Extending a Thread, defining run() method is not compulsory
       public void run() {
             System.out.println("Bike is running");

public class ThreadCar implements Runnable{
       //Implementing a Runnable, defining run() method is compulsory
       public void run() {
             System.out.println("Car is running");

public class ThreadExample {
       public static void main(String[] args) {
             //Example How to start a thread using a class, which extends Thread class
             ThreadBike bike = new ThreadBike();

             //Example How to start a thread using a class, which implements Runnable interface
             ThreadCar car = new ThreadCar();
             Thread thread =  new Thread(car);

Thread’s Constructors – Instantiating a Thread

Thread class provide us many variations of its constructor which we can use to define a new thread.

  • Thread()
  • Thread(Runnable target)
  • Thread(Runnable target, String name)
  • Thread(String name)
  • Thread(ThreadGroup group, Runnable Target)
  • Thread(ThreadGroup group, Runnable Target, String name)
  • Thread(ThreadGroup group, Runnable Target, String name, long stackSize)
  • Thread(ThreadGroup group, String name)

All of the above constructors create a new Thread instance, with following parameters–

  • Runnable target instance (the job, remember?)
  • String name as thread’s name
  • ThreadGroup group as instance of ThreadGroup which defines a group of threads
  • stackSize which defines the size of stack memory for a thread

Start Method – Starting a thread.

We know we have to write code inside run() method, which we want to execute in a thread, then we have to create an instance of Thread class using Thread class’s constructor which will be used to start the execution of thread. After that start() method is used, which is like a key to the car, it starts the actual process and converts a Thread class instance to a “Thread of Execution” by bringing it alive. Now new thread has its own stack.

Important Points to Remember

  • The start() method is called on the Thread’s instance, not on the Runnable’s instance.
  • If we call run() method directly, it won’t be called as starting a thread, instead it will run as a normal method call in the same stack of calling thread.
  • Thread.currentThread().getName() can be used to get the name of current thread. As we have explained above, we can set thread name while instantiating a Thread, this method should return the same name. If we didn’t set the name, it will still give the thread’s name which could be any random value.
  • getId() will give a unique, positive, long number as thread’s id.

That’s all for now, we will bring the “Thread’s Life cycle” as the next topic. Till that time just stay tuned. You can also share our post on social networks. Like our Facebook page or follow us on Twitter.

Anurag loves to learn new tech and share knowledge. He thinks learning is a never ending process and it must be free for all, one must share his knowledge with others. Other than his regular job in an IT giant, he loves to read autobiographies, thriller and fiction novels. You can reach him at anuragsati123[at]

You may also like...