Java Multi-Threading Question:

Explain the method of Runnable interface with example?

Tweet Share WhatsApp

Answer:

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();
}
}

Download Java Multi-Threading PDF Read All 36 Java Multi-Threading Questions
Previous QuestionNext Question
What are the advantages or usage of threads in Java?What are the methods of the thread class used to schedule the threads?