Creating Threads in Java

Threads help a single program to have multiple execution paths or multiple flow of control. Each flow of control can be thought of as a separate program known as thread, that runs concurrently with other such threads.
Creating threads in Java is implementing the run() method, which is the heart and soul of any thread. The run() method will have our entire business logic of the thread. The run() method is initiated with the help of another method called start().
There are two ways of creating a thread in Java:
- Implementing the Runnable interface.
- Extending the Thread class.
Note that if our class needs to extend another class, we have to create a thread by Implementing the Runnable Interface, since a Java class cannot have two superclass.

In this post we will see how to create a Thread by Implementing the Runnable Interface.
Follow the Steps below to create threads using the Runnable Interface:
1. Create a class implementing the Runnable Interface.
2. Implement the run() method in the class. This method will contain the business logic of the thread.
3. Create an Object of the  java.lang.Thread class by passing a Runnable object(of class created in Step 1) as argument to the Thread constructor.
4. Invoke the thread's start() method on the Thread object created in the Step 3.

Example program:
class Test1 implements Runnable { //step1
 private String threadName;
 Test1(String thrdName) {
  threadName  = thrdName;
 public void run() {    //Step2
          for(int i = 1; i <= 5; i++) {
             System.out.println("Thread Counter " + threadName + ": " + i);
//  Un-comment below line if you want to get info about the current thread
//             System.out.println(Thread.currentThread());
  System.out.println("Thread " + threadName  + " ends...");

public class RunnableTest1 {
 public static void main(String[] args) {
  Test1 runnable = new Test1("Testing-A");
  Thread thrd = new Thread(runnable);  //Step3
  thrd.start();       //Step4
  new Thread(new Test1("Testing-Z")).start(); //Start another thread, Step3 and Step4 combined
  System.out.println("Main program-thread ends...");

RunnableTest1 Output:
Thread Counter Testing-A: 1
Thread Counter Testing-A: 2
Thread Counter Testing-A: 3
Main program-thread ends...
Thread Counter Testing-A: 4
Thread Counter Testing-A: 5
Thread Testing-A ends...
Thread Counter Testing-Z: 1
Thread Counter Testing-Z: 2
Thread Counter Testing-Z: 3
Thread Counter Testing-Z: 4
Thread Counter Testing-Z: 5
Thread Testing-Z ends...

Example program:
class Test2 implements Runnable { // Step1
 private Thread thrd;

 Test2() { // no args constructor

 public Test2(String thrdName) {
  thrd = new Thread(this, thrdName); // Step3
  System.out.println("Starting thread " + thrd.getName());
  thrd.start(); // Step4

 public void run() { // Step2
  for (int i = 1; i <= 5; i++) {
   System.out.println("Thread Counter: "
     + Thread.currentThread().getName() + " - " + i);
  System.out.println("Thread " + Thread.currentThread().getName() + " ends...");

public class RunnableTest2 {
 public static void main(String[] args) {
  Test2 testThread1 = new Test2("Testing-A");

  // Same as previous program(RunnableTest1), but this will call the
  // no-args constructor
  Thread testThread2 = new Thread(new Test2(), "Testing-Z"); // Step3
  testThread2.start(); // Step4
  System.out.println("Main program-thread ends...");
RunnableTest2 Output:
Starting thread Testing-A
Main program-thread ends...
Thread Counter: Testing-A - 1
Thread Counter: Testing-Z - 1
Thread Counter: Testing-Z - 2
Thread Counter: Testing-Z - 3
Thread Counter: Testing-Z - 4
Thread Counter: Testing-Z - 5
Thread Testing-Z ends...
Thread Counter: Testing-A - 2
Thread Counter: Testing-A - 3
Thread Counter: Testing-A - 4
Thread Counter: Testing-A - 5
Thread Testing-A ends...

No comments:

Post a Comment