Implementing Runnable Interface-Java


•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.

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 :

Threadaimtocode: 1
Threadaimtocode: 2
Threadaimtocode: 3
Threadaimtocode: 4