Threads in Java : Create, start and run with examples

A Java program can be split into multiple subtasks and two or more subtasks can be executed concurrently thereby utilizing the CPU time to the maximum. Each subtask is executed in what is known as a Thread. In this article I will show you how to create a thread in Java and how to start and run threads.

Threads are implemented in Java using the java.lang.Thread class. There are two ways to create a thread in Java. One method is to subclass the Thread class and another method is to create a class that implements the Runnable interface and pass the Runnable class object to the constructor of Thread. Both these methods are explained with examples next.

Subclass Thread method

In this method of creating threads we create a class that extends the Thread class.

public class SubClassThread extends Thread{

}

In the main() method we create a new instance of SubClassThread() and invokes the start() method to start thread exectuion. Let's also print a message to the console to say that the thread is created.

public static void main(String args[]){  
		  
	Thread t = new SubClassThread();
	t.start();
	System.out.println("I created a thread");  
}  

We must implement the run() method which will override the run() inside the Thread class. In this example we will just print a message to the console when the thread runs.

public void run(){  
	    System.out.println("Hello, I am a thread");  
}  

Here is the whole program:

public class SubClassThread extends Thread{
	
    public void run(){  
	System.out.println("Hello, I am a thread");  
    }  
    
    public static void main(String args[]){  
	Thread t = new SubClassThread();
	t.start();
	System.out.println("I created a thread");  
    }  

}

Runnable Interface method

In case of Runnable interface method, you create a class that implements Runnable interface.

public class RunnableThread implements Runnable{
}

The run() method is exactly the same as in subclass method

public void run(){  
       System.out.println("Hello, I am a thread");  
}  

In the main() method you create a new instance of your class (rt) and pass the class object as a parameter to the Thread constructor (Thread(rt) ). The start method starts the execution of the thread.

public static void main(String args[]){  
		 
	RunnableThread rt = new RunnableThread();
	Thread t = new Thread(rt);
	t.start();
	System.out.println("I created a thread");  
}  

Here is the whole program

public class RunnableThread implements Runnable{
	
	@Override
	public void run(){  
		System.out.println("Hello, I am a thread");  
	}  

	  public static void main(String args[]){  

		RunnableThread rt = new RunnableThread();
		Thread t = new Thread(rt);
		t.start();
		System.out.println("I created a thread");  
	 }  

}

Subclass vs Runnable Thread

With the subclass method the class that implements your task will be a descendant of the Thread class which can be a limitation in some cases for example if you want task class to be extended from another class such as ServerSocket. Also you should be careful not to override other methods of the Thread class when using this method. The Runnable thread method is considered more flexible and separates the thread from the task. Apart from these there is no other reason why should or should not use one or the other.


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

Nothing yet..be the first to share wisdom.