Java Runnable Interface17 Mar 2025 | 4 min read Java runnable is an interface used to execute code on a concurrent thread. It is an interface which is implemented by any class if we want that the instances of that class should be executed by a thread. The runnable interface has an undefined method run() with void as return type, and it takes in no arguments. The method summary of the run() method is given below-
The runnable interface provides a standard set of rules for the instances of classes which wish to execute code when they are active. The most common use case of the Runnable interface is when we want only to override the run method. When a thread is started by the object of any class which is implementing Runnable, then it invokes the run method in the separately executing thread. A class that implements Runnable runs on a different thread without subclassing Thread as it instantiates a Thread instance and passes itself in as the target. This becomes important as classes should not be subclassed unless there is an intention of modifying or enhancing the fundamental behavior of the class. Runnable class is extensively used in network programming as each thread represents a separate flow of control. Also in multi-threaded programming, Runnable class is used. This interface is present in java.lang package. Implementing RunnableIt is the easiest way to create a thread by implementing Runnable. One can create a thread on any object by implementing Runnable. To implement a Runnable, one has only to implement the run method. public void run() In this method, we have the code which we want to execute on a concurrent thread. In this method, we can use variables, instantiate classes, and perform an action like the same way the main thread does. The thread remains until the return of this method. The run method establishes an entry point to a new thread. How to create a thread using Runnable interfaceTo create a thread using runnable, use the following code- The thread will execute the code which is mentioned in the run() method of the Runnable object passed in its argument. A simple thread example using runnableOutput: ![]() Use of Runnable class in network programmingThe runnable class is used to perform multi-thread programming, especially in server-side as a server may be getting several requests from different clients. To tackle this in a fast and resource-efficient way, we use multi-thread programming. Example of a networking program using Runnable- The following program shows a server program which creates a thread, then creates a socket and waits for a client to connect to it and asks for an input string- In this program, we are creating a socket for the client on a thread. Usually, different threads are created in a server for different client requests. Though, it is not a good practice to make an individual thread for each client if the compiler's ability to control per-thread memory is bad. Thread vs. Runnable![]() There are several differences between Thread class and Runnable interface based on their performance, memory usage, and composition.
Next TopicJava Tutorial |
CRC Program in Java
CRC stands for Cyclic Redundancy Check. It was invented by W. Wesley Peterson in 1961. It is an error detecting technique through which we can detect the error in digital networks(or communication channel or digital data) and storage devices. It is used to trace the accidental...
5 min read
Undulating Number in Java
In this section, we will learn what is an undulating number and also create Java programs to check if the given number is a undulating number or not. The undulating number program is frequently asked in Java coding interviews and academics. Undulating Number An undulating number is a...
3 min read
How to Accept Date in Java
? Java is a programming language that is frequently employed for creating a wide range of applications. Accepting user input is one of the most frequent jobs in any programme. In this section, we will discuss how to accept dates in Java. A fundamental data type in every...
4 min read
ThaiBuddhistDate now(Clock) method in Java with Example
The java.time.chrono.ThaiBuddhistDate is a class containing a now() as a function. The ThaiBuddhistDate class is used to retrieve the current ThaiBuddhist date from the given clock that is compatible with the ThaiBuddhist calendar system. Syntax: public static ThaiBuddhistDate now(Clock clock) Parameter: In accordance with this method, the Thai...
2 min read
POJO
in Java stands for Plain Old Java Object. It is an ordinary object, which is not bound by any special restriction. The file does not require any special classpath. It increases the readability & re-usability of a Java program. s are now widely accepted...
6 min read
Association Vs. Aggregation Vs. Composition in Java
Linking classes together is a crucial component of object-oriented programming (OOP), a complex web of interconnectivity. The three basic ideas that underpin these interactions are introduced by Java, a language well-known for its object-oriented principles: association, aggregation, and composition. These ideas not only serve as the...
8 min read
Two Decimal Places Java
In Java, when we use a double data type before a variable it represents 15 digits after the decimal point. But sometimes we require only two decimal places after decimal points like to represent rupees and other units. So, in this section, we are going to...
3 min read
Calculating Batting Average in Java
Batting average is a crucial statistic in the game of cricket that represents a player's performance at the crease. It measures the player's ability to score runs consistently and is one of the most widely used metrics for evaluating a batter's proficiency. In this article, we...
4 min read
What is Retrieval Operation in ArrayList Java
? ArrayList is a versatile data structure in Java that provides a dynamic array implementation, offering flexibility in storing and manipulating collections of objects. Among the various operations available in ArrayList, the retrieval operation plays a crucial role. It enables developers to access specific elements based on...
4 min read
Median Of Stream Of Running Integers in Java
An integer array is given to us. Compute the median of the elements traversed so far in the input array. For the sake of simplicity, assume that there are no duplicates. Example: Input int arr[] = {17, 11, 15, 13, 10, 12, 18, 19, 1, 16, 14, 20}; Output: {17,...
11 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

