answersLogoWhite

0

A Runnable Interface is one that is used to create a Java Thread...

A Thread can be created in two ways and using the Runnable Interface is one of them.

Example:

public class Test implements Runnable {

public void run(){

....

}

}

The Runnable interface would have an abstract instance of the method run() which needs to be implemented in the class which wants to create a Thread.

User Avatar

Wiki User

16y ago

Still curious? Ask our experts.

Chat with our AI personalities

BeauBeau
You're doing better than you think!
Chat with Beau
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
More answers

Runnable in java , is an interface which helps in creating threads.

E.g. you want to create thread Demo , then you will do like this

class Demo implement Runnable

{

// your code

}

Now to start thread in a program you have to do like this

/* create your thread class object

& then pass it to thread class constructor */

Demo dmo = new Demo();

Thread thrd = new Thread(dmo);

// call start() using thrd

thrd.start();

thats all . you can add additional code as your requirements

User Avatar

Wiki User

9y ago
User Avatar

  • Java Runnable Thread is a piece of the program execution.
  • Java Runnable is an interface. Thread class implements it.
  • Java has multithreading facility.
  • Thread is also created by implementing the Runnable interface

Java Runnable Thread Example

public class runnable1 implements Runnable {

@Override

public void run() {

for (int x = 1; x <= 3; x++) {

System.out.println(x + " Thread name, priority and group are "

+ Thread.currentThread()); }

}

public static void main(String[] args) {

runnable1 run1 = new runnable1();

Thread t1 = new Thread(run1);

t1.start();

}

}

User Avatar

Wiki User

11y ago
User Avatar

The Runnable Interface is used to create threads in Java. Implementing the Runnable interface gives you a way to extend any class you like, but still define behavior that will be run by a separate thread. It looks like this:

class MyFirstRunnableClass implements Runnable {

public void run() {

System.out.println("Imp job running in MyFirstRunnableClass");

}

}

Regardless of which mechanism you choose, you've now got yourself some code that can be run by a thread of execution. So now let's take a look at instantiating your thread-capable class, and then we'll figure out how to actually get the thing running.

User Avatar

Wiki User

13y ago
User Avatar

Runnable is an interface in Java that is used to implement Multithreading in Java. This interface has a method run() which needs to be implemented by the class using this interface.

User Avatar

Wiki User

15y ago
User Avatar

interface need for communication

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Why do you need runnable interface?
Write your answer...
Submit
Still have questions?
magnify glass
imp