Java Multi-Threading Question:
Download Job Interview Questions and Answers PDF
Explain the method of Runnable interface with example?
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();
}
}
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 Interview Questions And Answers
PDF
Previous Question | Next Question |
What are the advantages or usage of threads in Java? | What are the methods of the thread class used to schedule the threads? |