Menu
Topics Index
...
`

Thread with Runnable - Java Example Program


Thread with Runnable
class RunnableExample
{
    public static void main(String[] args)
    {
    
        Runnable r = new MyThread();
        Thread t = new Thread(r);
        System.out.println("Created thread by implementing Runnable interface.");
        t.start();
    
    }

}

class MyThread implements Runnable
{
    public void run()
    {
        System.out.println("Thread Running...");
    }
}
OUTPUT

Created thread by implementing Runnable interface.
Thread Running...

DESCRIPTION

In this example we created an object of type Runnable. We used the constructor Thread(Runnable obj) to create thread. When thread's start method is invoked it automatically invokes thread's run method.

THINGS TO TRY
  • Created a thread and invoke start method use below code if necessary.
    Thread t1 = new Thread(r);
    t1.start();
  • Change the text "Thread Running..." to "I am run method." in run method and invoke start method on thread.

© meritcampus 2019

All Rights Reserved.

Open In App