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.
- 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
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
- public interface Runnable {
- void run();
- }
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:
- Class MyRunnable implements Runnable {
- }
Class MyRunnable implements Runnable { }
Step 2
Provide the definition for “run()” method with the actual logic that thread has to execute
Example:
- Class MyRunnable implements Runnable {
- public void run(){
- //code that will run for each new thread
- }
- }
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.
- 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.
- }
- }
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:
- Class MyThread extends Thread {
- }
Class MyThread extends Thread { }
Step 2
Provide the definition for “run()” method with the actual logic that thread has to execute
Example:
- Class MyThread extends Thread {
- public void run(){
- //code that will run for each new thread
- }
- }
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
- 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();
- }
- }
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
- 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();
- }
- }
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(); } }