Implementing Runnable Interface-Java

aimtocode.COM

Definition

The runnable interface calls run() method to implement threads in our program.

Runnable interface is the primary template for any object that is intended to be executed by a thread. It defines a single method run(), which is meant to contain the code that is executed by the thread.
java.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread.


aimtocode.COM

Example 1:



Output :

Main thread is- main
Thread-0, executing run() method!

Example 2:



Output :

Inside : main
Creating Runnable...
Creating Thread...
Starting Thread...
Inside : Thread-0

Example 3:



Output :

ThreadPvpne: 1
ThreadPvpne: 2
ThreadPvpne: 3
ThreadPvpne: 4
aimtocode.COM