Creating and starting Threads


Thread creation in Java ?


We learned in our previous article that “ thread ” is an instance of “Thread” class.

Please read Introduction-to-threads article before reading further.

Threads can be created using any one of the 2 ways


1) Implementing the java.lang.Runnable interface.

2) Extending the java.lang.Thread class

In both the ways, we need to override run() method and provide the business logic inside run() method which will be called automatically when we start a thread.

We use Thread class constructor to create a new thread and some of them are as below

Thread()

Thread(String name)

Thread(Runnable r)

Thread(Runnable r,String name)


How to start a thread ?


When we create a new thread, it won’t execute run() method

When we start the thread then only run() method will be executed.

Thread can be started by invoking “ start () ” method.

  1.  MyThread t1=new MyThread();  //Creating new thread/(by extending Thread class), this line won’t start a thread
  2.  
  3. t1.start(); //This will automatically invoke run() method
 MyThread t1=new MyThread();  //Creating new thread/(by extending Thread class), this line won’t start a thread

t1.start(); //This will automatically invoke run() method


Lets understand 2 ways of creating Threads

1) Implementing the java.lang.Runnable interface.

Runnable interface has only one abstract method

  1. public interface Runnable {
  2.     void run();
  3. }
public interface Runnable {
	void run();
}


The Runnable interface has only one method called “ run () “ method.

If we want to create a thread using Runnable interface, we need to follow below steps

Step 1

Create a new class and implement Runnable interface for this new class

Example:

  1. Class MyRunnable implements Runnable {
  2.  
  3. }
Class MyRunnable implements Runnable {

}

Step 2

Provide the definition for “run()” method with the actual logic that thread has to execute

Example:

  1. Class MyRunnable implements Runnable {
  2. public void run(){
  3. //code that will run for each new thread
  4. }
  5. }
Class MyRunnable implements Runnable {
public void run(){
//code that will run for each new thread
}
}

Step 3

Start the thread

We need to create the object of MyThread class as below

MyRunnable myRunnableObj = new MyRunnable ();


We need to create the object of Thread class as below

Thread t1 = new Thread(myRunnableObj);


Now we can start the thread using Thread class object as below

t1.start();


This will invoke the run() method of MyRunnable class

2) Extending the java.lang.Thread class

Thread class is a special class written to achieve Thread feature in Java.

Without creating an object of this class, we can’t create thread in Java

Thread class provides various methods like run(), start (), stop (), join () etc.

Thread class extends Object class and also implements Runnable interface.

  1. class Thread extends Object implements Runnable {
  2.     Public void run(){
  3.  
  4. // If the thread was constructed using a separate Runnable run object, then that Runnable object's
  5. //run method is called; otherwise, this method does nothing and returns.
  6.  
  7. }  
  8. }
class Thread extends Object implements Runnable {
	Public void run(){

// If the thread was constructed using a separate Runnable run object, then that Runnable object's 
//run method is called; otherwise, this method does nothing and returns.

}	
}


If we want to create a thread by extending Thread class, we need to follow below steps

Step 1

Create a new class and extend Thread class for this new class

Example:

  1. Class MyThread extends Thread {
  2.  
  3. }
Class MyThread extends Thread {

}

Step 2

Provide the definition for “run()” method with the actual logic that thread has to execute

Example:

  1. Class MyThread extends Thread {
  2. public void run(){
  3.    //code that will run for each new thread
  4. }
  5. }
Class MyThread extends Thread {
public void run(){
   //code that will run for each new thread
}
}

Step 3

Start the thread

We need to create the object of MyThread class as below

MyThread myThreadObj = new MyThread ();


Now we can start the thread using MyThread class object as below

myThreadObj.start();


This will invoke the run() method of myThreadObj class

Let’s see thread creation in both the ways with complete example

1) Thread creation by implementing Runnable interface

  1. class MyRunnable implements Runnable{  
  2. public void run(){  
  3. System.out.println("Thread is running...");  
  4. }
  5. }  
  6.  
  7. public class ThreadManager {
  8. public static void main(String args[]){  
  9. MyRunnable myRunnableObj=new MyRunnable();  
  10. Thread t1 =new Thread(myRunnableObj);  
  11. t1.start();  
  12.  }  
  13. }  
class MyRunnable implements Runnable{  
public void run(){  
System.out.println("Thread is running...");  
}
}  
 
public class ThreadManager { 
public static void main(String args[]){  
MyRunnable myRunnableObj=new MyRunnable();  
Thread t1 =new Thread(myRunnableObj);  
t1.start();  
 }  
}  



2) Thread creation by extending Thread class

  1. class MyThread extends Thread{  
  2. public void run(){  
  3. System.out.println("Thread is running...");  
  4. }
  5. }
  6. public class ThreadManager {
  7. public static void main(String args[]){  
  8. MyThread t1=new MyThread();  
  9. t1.start();  
  10.  }  
  11. }  
class MyThread extends Thread{  
public void run(){  
System.out.println("Thread is running...");  
} 
}
public class ThreadManager { 
public static void main(String args[]){  
MyThread t1=new MyThread();  
t1.start();  
 }  
}  


About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on