answersLogoWhite

0

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

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What java interface must be implemented by all threads?

Runnable interface


Why do you need runnable interface?

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.


Can interface implement the other interface?

No. An interface cannot implement another interface, it can only just extend it. Because, an interface cannot implement any method as it has no method body declarations.


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


What are the different ways of implementing thread in java?

You can define and instantiate a thread in one of two ways: • Extend the java.lang.Thread class. • 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: • Extend the java.lang.Thread class. • 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: • Extend the java.lang.Thread class. • 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"); } }


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.


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 { ... }


What is AppletStub Interface?

The applet stub interface provides the means by which an applet and the browser communicate. Your code will not typically implement this interface


Which interface must you implement to ensure that your Servlet is single threaded?

SingleThreadModel