Creating a Thread by Implementing Runnable Interface(continues)
Table of Contents
- Introduction
- What is the Runnable Interface?
- Creating Threads in Java
- Implementing Runnable Interface for Threads
- Example: Thread Creation by Implementing Runnable
- Step-by-Step Code Explanation
- 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.
1 2 3 |
public interface Runnable { void run(); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.studyeasy; import java.util.Random; import static java.lang.Thread.sleep; public class Main { public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { try { Random random = new Random(); sleep(random.nextInt(1000)); // Random sleep time between iterations } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(i); // Print iteration count } } }).start(); // Start the thread } } |
6. Step-by-Step Code Explanation
Let’s break down the code to understand how it works:
- 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. - Creating a Thread:
Inside the main() method, a new Thread object is created. The Thread constructor accepts a Runnable object as an argument. - 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. - 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. - 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:
1 2 3 4 5 6 7 8 9 10 |
0 1 2 3 4 5 6 7 8 9 |
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.