Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Creating a Thread by Implementing Runnable Interface in Java

 

In this lesson, you will learn.

  • Creating a Thread by Implementing Runnable Interface
  • Example

 

Creating a Thread by Implementing Runnable Interface

As we stated earlier, Threads can be created in two ways

  1. By Extending aThread
  2. By Implementing a Runnable Interface

 

In this lesson, you will learn the second approach to create a Thread

Creating a new thread by Implementing a Runnable Interface includes the following 3 steps.

 

Step 1: Create a Thread Class That Implements Runnable Interface

class Counter implements Runnable {
    // Body of a Class
}

 

Step 2: Overriding run() Method

@Override
	public void run() {
		//Body of the Thread
	}

 

Step 3: Creating Thread Object Passing the Reference of A Thread Class and Call Start Method

Counter counter1 = new Counter("Counter 1");
Thread t1 = new Thread(counter1);
t1.start();

 

Example: Creating Thread by Implementing Runnable Interface

package usingrunableinterface;

//Define a class that implements the Runnable interface
class Counter implements Runnable {
 private String name;

 public Counter(String name) {
     this.name = name;
 }

 // The run method contains the code that will be executed by the thread
 public void run() {
     for (int i = 1; i <= 5; i++) {
         try {
             // Sleep for a second to simulate work
             Thread.sleep(1000);
         } catch (InterruptedException e) {
             System.out.println(name + " has been interrupted.");
         }
         System.out.println(name + ": " + i);
     }
     System.out.println(name + " has finished counting.");
 }
}

public class ThreadUsingRunnable {
 public static void main(String[] args) {
     // Create instances of the Runnable object
     Counter counter1 = new Counter("Counter 1");
     Counter counter2 = new Counter("Counter 2");

     // Create two threads from those instances
     Thread thread1 = new Thread(counter1);
     Thread thread2 = new Thread(counter2);

     // Start the threads
     thread1.start();
     thread2.start();

     try {
         // Wait for both threads to finish
         thread1.join();
         thread2.join();
     } catch (InterruptedException e) {
         System.out.println("Main thread was interrupted.");
     }
     System.out.println("Main thread has finished.");
 }
}

Output: First Run

Counter 2: 1
Counter 1: 1
Counter 1: 2
Counter 2: 2
Counter 2: 3
Counter 1: 3
Counter 1: 4
Counter 2: 4
Counter 2: 5
Counter 1: 5
Counter 2 has finished counting.
Counter 1 has finished counting.
Main thread has finished.

 

Output: Second Run

Counter 1: 1
Counter 2: 1
Counter 1: 2
Counter 2: 2
Counter 2: 3
Counter 1: 3
Counter 1: 4
Counter 2: 4
Counter 1: 5
Counter 2: 5
Counter 2 has finished counting.
Counter 1 has finished counting.
Main thread has finished.

 

Explanation:

  • Counter Class: This class implements the Runnable interface, which requires the implementation of the run() method. The name attribute is used to identify the thread in the output.
  • run() Method: This method outlines what the thread will execute. Here, it counts from 1 to 5, pausing for a second between each count, and prints out each count.
  • Main Method: In the main method, two Counter objects are created, each initialized with a different name.
  • These are then used to create two Thread instances. Both threads are started with the start() method.
  • The join() method is called on each thread to ensure that the main thread waits for both of these threads to complete before it continues and prints its final message.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review