S12L05 – Creating thread by implementing runnable interface continues

Creating a Thread by Implementing Runnable Interface(continues)

Table of Contents

  1. Introduction
  2. What is the Runnable Interface?
  3. Creating Threads in Java
  4. Implementing Runnable Interface for Threads
  5. Example: Thread Creation by Implementing Runnable
  6. Step-by-Step Code Explanation
  7. Conclusion

1. Introduction

In Java, the concept of multithreading allows a program to execute multiple threads concurrently, enabling efficient use of CPU resources. In this article, we will explore how to create a thread by implementing the Runnable interface in Java. Understanding how to work with threads is crucial for developing high-performance applications, such as web servers, game development, and database applications.

By the end of this article, you will gain a clear understanding of:

  • What the Runnable interface is
  • How to create and start threads in Java using this interface
  • Step-by-step explanation of a sample implementation

2. What is the Runnable Interface?

The Runnable interface in Java is a functional interface that is part of the java.lang package. It represents a task that can be executed by a thread. This interface contains a single method, run(), which contains the code that the thread will execute.

3. Creating Threads in Java

There are two primary ways to create a thread in Java:

  • By extending the Thread class
  • By implementing the Runnable interface

Using the Runnable interface is often preferred when the class needs to extend another class, as Java does not support multiple inheritance. This approach also enhances code modularity.

4. Implementing Runnable Interface for Threads

When implementing the Runnable interface, the class needs to provide an implementation
for the run() method. The run() method should contain the task or operations the thread will perform. The thread itself is created and started using the Thread class.

5. Example: Thread Creation by Implementing Runnable

Let’s look at an example where we create a thread using the Runnable interface.
Here is a simplified Java program:

6. Step-by-Step Code Explanation

Let’s break down the code to understand how it works:

  1. Package Declaration and Imports:
    The code starts by importing required packages, including java.util.Random
    for generating random numbers and the Thread.sleep() method for adding delays in execution.
  2. Creating a Thread:
    Inside the main() method, a new Thread object is created. The Thread constructor accepts a Runnable object as an argument.
  3. Implementing Runnable:
    The Runnable interface is implemented using an anonymous inner class, which overrides the run() method to define the task to be performed by the thread.
  4. Task Logic:
    The run() method includes a loop that iterates ten times, where each iteration prints the value of i (the iteration count) after a random delay between 0 to 1000 milliseconds.
  5. Starting the Thread:
    The start() method of the Thread class is called to begin the execution of the run() method in a separate thread.

Output:

7. Conclusion

In this article, we explored how to create threads by implementing the Runnable interface in Java. This method provides flexibility when developing concurrent applications, particularly when multiple inheritance is a concern. By using the Runnable interface, we can define tasks that are executed in separate threads without restricting the class from extending other classes.