Java Runnable Interface

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-

MethodDescription
public void run()This method takes in no arguments. When the object of a class implementing Runnable class is used to create a thread, then the run method is invoked in the thread which executes separately.

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 Runnable

It 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 interface

To 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 runnable

Output:

Java Runnable Interface

Use of Runnable class in network programming

The 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

Java Runnable Interface

There are several differences between Thread class and Runnable interface based on their performance, memory usage, and composition.

  • By extending thread, there is overhead of additional methods, i.e. they consume excess or indirect memory, computation time, or other resources.
  • Since in Java, we can only extend one class, and therefore if we extend Thread class, then we will not be able to extend any other class. That is why we should implement Runnable interface to create a thread.
  • Runnable makes the code more flexible as, if we are extending a thread, then our code will only be in a thread whereas, in case of runnable, one can pass it in various executor services, or pass it to the single-threaded environment.
  • Maintenance of the code is easy if we implement the Runnable interface.

Next TopicJava Tutorial