answersLogoWhite

0


Best Answer

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

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

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

This answer is:
User Avatar

User Avatar

Wiki User

11y ago
  • 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();

}

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

interface need for communication

This answer is:
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
Continue Learning about Engineering

What java interface must be implemented by all threads?

Runnable interface


If Runnable interface is better than Thread class than why you are using Thread class What is the need for Thread class?

Though implementing Runnable interface is better approach than inheriting from Thread class but there are certain methods available in Thread class like join,sleep or yield which does not available in Runnable interface and programmer can not use them unless he has object of Thread class. This is why we should have Thread class Object. Rupesh Raghani


How many methods in runnable interface?

chase that feeling like ive been looking for something thats good for the rich and the blind and the poor


Describe a simple scheme in which there are as many lightweight processes as there are runnable threads?

Start with only a single LWP and let it select a runnable thread. When arunnable thread has been found, the LWP creates another LWP to look for anext thread to execute. If no runnable thread is found, the LWP destroys itself.


What is the Difference between header file and package?

A package is just a mechanism for grouping objects, it is very similar to grouping items within a folder or directory on a file system. A class is found within a package, but this does not have an impact on the class' behavior (review the "package" access level for a slight exception). An interface, however, is a .java file that is used (implemented) by another class to tell the outside world that it conforms to a certain specification. For example, you might have a "Runnable" interface that has a "run()" method in it, by having a class that is "Runnable" (implements Runnable) anyone using that class knows that it must have a "run()" method defined. This is used when you have several different classes that have the same interface. Interfaces have more in common with abstract classes than they do with packages. An interface, by definition, cannot have any implemented methods; an abstract class, in contrast, can define some methods and leave some methods to be implemented by a subclass. Also, a class can implement many interfaces, but can only extend one (abstract) class. Answer by Sahe Alam Ansari, BCA. Nepal bhairahawa. Email: mrsahealam@gmail.com

Related questions

How do you implement runnable interface?

By implementing Runnable in our class and by overriding the run() method of Runnable interface


What java interface must be implemented by all threads?

Runnable interface


If Runnable interface is better than Thread class than why you are using Thread class What is the need for Thread class?

Though implementing Runnable interface is better approach than inheriting from Thread class but there are certain methods available in Thread class like join,sleep or yield which does not available in Runnable interface and programmer can not use them unless he has object of Thread class. This is why we should have Thread class Object. Rupesh Raghani


How many methods in runnable interface?

chase that feeling like ive been looking for something thats good for the rich and the blind and the poor


What are the two ways of spawning a thread in Java?

You can create a Thread in Java by using two ways. 1. Extending the Thread class public class Test extends Thread { ..... } 2. Implementing the Runnable Interface public class Test implements Runnable { ... }


Why do you use an interface for implementing methods Isn't it just extra work?

== == By creating an interface, the programmer creates a method to handle objects that implement it in a generic way. An example of this would be the Runnable interface in Java. If the programmer is given a Runnable object, they do not need to know what that object really is to be able to call the run method. Ex. public void startThreads(ArrayList threads){ for(Runnable r : threads) (new Thread(r)).start(); } The objects in the ArrayList threads may be anything at all, but because they all implement the Runnable interface, it is possible to execute their run method generically, in this case encapsulating it in a Thread object and starting that Thread. Answer: In Java, it will not support a concept like "subclass having more than one superclass" ex: class subclass extends superclass1,superclass2 so, to implement this concept , Java introduced a new methodology called "interface", interface is also a class , but it has only function declarations having no function definitions. thereby, we can inherit more than one interfaces through "implements" key word. For an interface we cont declare an object,but, we have an alternative ,through assigning an object of a class which implements that interface. ex: interfacename iobj; subclass sobj=new subclass(); iobj=sobj; iobj.funame1(); iobj.funame2(); iobj.funame3(); note: these three funames are the member functions of the above interface, and subclass has to give the definition for them, dynamic binding...right! Apart from these already three declared functions, we don't give the reference to the iobj, ok! the other explanation for implements is, giving definition for a member function, today or in future ,which is declared in past.


What is the difference of interface and class?

A (concrete) class defines all his methods and can have any kind of instance variable and is declared with the 'class' keyword. Unlike an interface that doesn't implements any of his methods and his variables must be static and final. An interface is defined with the 'interface' keyword.//This is a classpublic class MyClass{public String name;private int age;public void myMethod(){//do something}}//This is a interfaceinterface MyInterface {public static final String name;public static final int age;public void myMethod(); // This method is not implemented}An other difference is that a class can be inheritance by other class but a interface must be implemented using the keyword 'implements'.interface Runnable(){void run();}public class MyOtherClass implements Runnable{public void run(){// insert interesting code here}}


What are the different ways of implementing thread in java?

You can define and instantiate a thread in one of two ways: &acirc;&euro;&cent; Extend the java.lang.Thread class. &acirc;&euro;&cent; Implement the Runnable interface. class MyFirstThread extends Thread { public void run() { System.out.println("Important job running in MyFirstThread"); } } or class MyFirstRunnableClass implements Runnable { public void run() { System.out.println("Imp job running in MyFirstRunnableClass"); } }


How do you make a Thread in java?

You can define and instantiate a thread in one of two ways: &acirc;&euro;&cent; Extend the java.lang.Thread class. &acirc;&euro;&cent; Implement the Runnable interface. Ex: class MyFirstThread extends Thread { public void run() { System.out.println("Important job running in MyFirstThread"); } } class MyFirstRunnableClass implements Runnable { public void run() { System.out.println("Imp job running in MyFirstRunnableClass"); } }


What are various ways to create a thread in java?

You can define and instantiate a thread in one of two ways: &acirc;&euro;&cent; Extend the java.lang.Thread class. &acirc;&euro;&cent; Implement the Runnable interface. Ex: class MyFirstThread extends Thread { public void run() { System.out.println("Important job running in MyFirstThread"); } } class MyFirstRunnableClass implements Runnable { public void run() { System.out.println("Imp job running in MyFirstRunnableClass"); } }


What cannot be renewable?

Wind and water are not a runnable resource.


Describe a simple scheme in which there are as many lightweight processes as there are runnable threads?

Start with only a single LWP and let it select a runnable thread. When arunnable thread has been found, the LWP creates another LWP to look for anext thread to execute. If no runnable thread is found, the LWP destroys itself.