-
Notifications
You must be signed in to change notification settings - Fork 14
java.lang Runnable Interface in Java
In this article, we will learn about Runnable interface and it's usage with examples.
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor.
package java.lang;
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
Note that Runnable interface is a functional interface so we can use lambda expression on Runnable interface.
The Runnable interface defines a single method, run, meant to contain the code executed in the thread.
- void run() - When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.
Let's demonstrate how to use Runnable interface with an example.
First, create a Task or WorkerThread using Runnable interface.
public class WorkerThread implements Runnable {
private String data;
public WorkerThread(final String anyData) {
this.data = anyData;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("[" + Thread.currentThread().getName() + "] [data=" +
this.data + "] Message " + i);
try {
Thread.sleep(200);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
}
Second, let's see how to use WorkedThread in main thread. in this example main() method is main thread which will create aonther using Above WorkerThread class.
public class InstantiateUsingRunnable {
public static void main(final String[] args) {
System.out.println("Thread main started");
final Thread thread1 = new Thread(new WorkerThread("Process data through Runnable interface"));
thread1.start();
thread1.setName("Demo Thread");
System.out.println("Thread main finished");
}
}
Output:
Thread main started
Thread main finished
[Demo Thread] [data=Process data through Runnable interface] Message 0
[Demo Thread] [data=Process data through Runnable interface] Message 1
[Demo Thread] [data=Process data through Runnable interface] Message 2
[Demo Thread] [data=Process data through Runnable interface] Message 3
[Demo Thread] [data=Process data through Runnable interface] Message 4
Note that, instead of creating a class which implements Runnable and then instantiating that class to get the runnable object, you can create an anonymous runnable by using Java’s anonymous class syntax.
Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time
public class RunnableExampleUsingAnonymousClass {
public static void main(final String[] args) {
System.out.println(" main thread started : " + Thread.currentThread().getName());
System.out.println("Creating Runnable...");
final Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Inside : " + Thread.currentThread().getName());
}
};
System.out.println("Creating Thread...");
final Thread thread = new Thread(runnable);
System.out.println("Starting Thread...");
thread.start();
System.out.println(" main thread ended : " + Thread.currentThread().getName());
}
}
Output:
main thread started : main
Creating Runnable...
Creating Thread...
Starting Thread...
main thread ended : main
Inside : Thread-0
The above example can be made even shorter by using Java 8’s lambda expression -
public class RunnableExampleUsingLambda {
public static void main(final String[] args) {
System.out.println(" main thread started : " + Thread.currentThread().getName());
System.out.println("Creating Runnable...");
final Runnable runnable = () -> System.out.println("Inside : " + Thread.currentThread().getName());
System.out.println("Creating Thread...");
final Thread thread = new Thread(runnable);
System.out.println("Starting Thread...");
thread.start();
System.out.println(" main thread ended : " + Thread.currentThread().getName());
}
}
Output:
Creating Runnable...
Creating Thread...
Starting Thread...
main thread ended : main
Inside : Thread-0