In this article, we will learn about the Runnable interface and its 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 run() method, which contains a code that executed in the thread. The Runnable object is passed to the Thread constructor.
Tóm Tắt
Runnable Interface Source Code in Java Library
package
java.lang
;@FunctionalInterface
public
interface
Runnable
{public
abstract
void
run
(); }
Runnable interface is a functional interface so we can use Runnable interface.
Note thatinterface is a functional interface so we can use Java 8 Lambda Expressions on ainterface.
Runnable Interface Method
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.
Runnable Interface Examples
Let’s demonstrate how to use Runnable interface with an example.
First, create a Task or WorkerThread using Runnable interface.
WorkerThread.java
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 WorkerThread in the main thread. in this example the main() method is the main thread which will create another 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
Runnable Interface Example using Anonymous Class
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
Runnable Interface Example Using Lambda Expression
The above example can be made even shorter by using Java 8 Lambda Expressions
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