Answers

Question and Answer:

  Home  Java Multi-Threading

⟩ Explain the method of Runnable interface with example?

In this method of creating thread, we have to implement the Runnable interface and implement the run() method in our class.

We have to create an object of our class.

Then we you have to pass the reference of that object for creating a new object of Thread

Invoke the start method using this Thread object which will create a new thread of execution.

For example

public class MyThread implements Runnable{

public void run(){

// code to execute under the thread

}

public static void main(String [] args){

MyThread c = new NewThread();

Thread t = new Thread(c);

t.start();

}

}

 168 views

More Questions for you: