What is difference between runnable and thread ?why we use static class

Showing Answers 1 - 49 of 49 Answers

Ravi

  • Jan 10th, 2006
 

Runnable is a interface and also used for to create threads.but the diff. is runnable interface is used for to create only simple threads.thread class is used for any extra activites need for one thread at the time this class will use.

Java cannot support the mltiinheritence.if use the Thread class to create ant thread at the same time u cant extends the any other class.suppose if u developed any application using AWT or SWING u must be extends JFrame class at the same time u cant extends thread class .so at that time we can go to the Runnable interface . Thanks vemodhara.k

static is used to declare the methods and varibles.It is access modifierwhen u declared the methods or varibles as static those all are activated(executed) at the time of class load into the JVM.And nonstatic varibles cannot be used in the static methods.and another thing is class having it's own copy varibles.directly we can call those by class name directly and through class instance also possible.regards vemodhara.k

  Was this answer useful?  Yes

Ravi

  • Jan 11th, 2006
 

Runnable is a interface that is used to create threads.Thread is a resource used to providing the procesess for request

  Was this answer useful?  Yes

Devyani

  • Jan 13th, 2006
 

Thread is a class and Runnable is an interface.
Runnable is implemented by Class thread.
To run threads, any class either has to extend "Thread" Class or has to implement Runnable interface.
Runnable interface contains "run()" method to start the action without any arguement.
Any class who implements "runnable", has to override "Run()" method.

  Was this answer useful?  Yes

Devyani

  • Jan 13th, 2006
 

Static members includes those variables and methods who have a keyword static before declaration.
Static members of any class are the members of only that class.These members can't be shared by the objects(instances) of that class.
i.e.Static members are not instance variables.

  Was this answer useful?  Yes

sudheer

  • Jan 14th, 2006
 

Thread is a class in and Runnable is an interface in java. A thread can be created in two ways. "java.lang" package gives us two classes for this purpose (i) Thread class (ii) Runnable interface. That is we can either Extending the Tread class or Immplement the Runnable Interface. Which one of the above methods is to be used depends on the programer's choice and his convinience.  

  Was this answer useful?  Yes

sudheer

  • Jan 14th, 2006
 

Static is a key word in java. Static can also be used as an access modifier with members(variables and methods) of a class. A static method belongs to the whole class ie. the objects of the class share the same static variables. Static variables do not have a "this" reference. "This is a key word to refer to the current object. We use static classes to support static members.

  Was this answer useful?  Yes

nagan

  • Jan 21st, 2006
 

Difference between Runnable interface and Thread comes with in the main() method.If our class is not extending another class we can use either extending Thread class or implementing Runnable interface.If our class is already extending another class we can't extend Thread class because java doesn't support multiple inheritance so we have left only one choice that is implementing Runnable interface.

nagan

  • Jan 21st, 2006
 

To call a global variable or methods we require object in the main method.

Static is a keyword of Java.Static is used for variables and methods.When we declared instance variables(Global variable) or methods as static this variable or method can be accessable with out help of object,with the help of object,with the help of class name.

Static variable is called "class variable" because static variable has only one location that means shared or accessed by all the objects of class i.e whole class accessed one location thats y static variable is called class variable.Local variables cant be static because it can't be accessed by object.

  Was this answer useful?  Yes

sbarik

  • Jan 27th, 2006
 

Declaring a class static has a significance in case of InnerClasses so that one can acess the ineerclass just by specifying NameOfOuterClass.NameOfInnerClass...

  Was this answer useful?  Yes

puneeth

  • Mar 16th, 2006
 

static is a keyword used to create:

1)class variables

2)static bolck

3)static methods

4)nested toplevel classes

static provides flexibilty to call the method without creating the object.

ex:static void println()

{

system.out.println("hii");

}

publicstatic

  Was this answer useful?  Yes

madan

  • Apr 3rd, 2006
 

  • both for creating threads but Runnable interface contain only one method that is run() method.if we dont require any other thred methods then implementing runnable interface,other wise extending thread class.

  Was this answer useful?  Yes

uafshahid

  • May 1st, 2006
 

Both Runnable and Thread are used to create new threads.

Runnable is an interface                                        Thread is a class

Runnable contains defnition of                             Thread contains run() method

run() method without                                              alongwith its implementation.

implementation                                                           

Whenever we will use Runnable interface to create thread we will provide the implementation of run method. But in the case of Thread class whenever we use this class to create thread we will override already written method of run.

Declaration Difference of Runnable and Thread:

public interface Runnable{

         public void run();

}

public class Thread{

        public void run(){

        }

}

 

  Was this answer useful?  Yes

p.pradeepu

  • Sep 30th, 2009
 

Runnable is a interface it contains only one method run(). void run().Thread class is implemented by runnable interface and also it contains run() but main difference is using runnable interface will create simple threads but thread class will use another activities like a thread is still running or not, will set thread names and priority etc.



final boolean isAlive( );
void run( )
final void setName(String thrdName)
etc.

  Was this answer useful?  Yes

bharath.334

  • Jan 22nd, 2010
 

Runnable interface internally uses Thread Class. If your class is not extending any other business classes as of now or wont extend in the future also then you can go for Thread class. But if your class has to extend some business class and also want to implement Thread feature means its better to go for Runnable interface. Because you can implement n number of interfaces you can go for Runnable.

  Was this answer useful?  Yes

msdmsd

  • Aug 14th, 2010
 

A Java Thread controls the main path of execution in an application. When you invoke the Java Virtual Machine with the java command, it creates an implicit thread in which to execute the main() method. The Thread class provides a mechanism for the first thread to start-up other threads to run in parallel with it.

The Runnable interface defines a type of class that can be run by a thread. The only method it requires is run(), which makes the interface very easy to fulfil by extending existing classes. A runnable class may have custom constructors and any number of other methods for configuration and manipulation.

  Was this answer useful?  Yes

jagadeeshyy

  • Aug 15th, 2010
 

Thread is a class and Runnable is an interface, these two are given by lang package. Static class is used when the data of the class shared among different objects.

  Was this answer useful?  Yes

                Runnable  Interface          Extending the thread
Support for  Multi Inheritance Not support for multi inheritance
As we know Interface Having Methods .Thread Extending is having the Complete Class.
Runnable is a interface it contains only one method run()Thread  class contain final boolean isAlive( );final void setName(String thrdName)
Runnable interface internally uses Thread Class , During the run time Direct execution of thread class .
Doesn’t have any constructers  Thread class have  constructers  

  Was this answer useful?  Yes

Runnable is an interface and Thread is a class which implements Runnable interface.
We can create the threads in two ways i.e by using Runnable interface and by using Thread class.

But the advantage with Runnable interface is multipel inheritance is possible where as with Thread class multiple inheritance is not possible.

i.e what ever the class which implements Runnable interface can also inherit multple interface but Where as  the class which extends Thread class should not support multiple inheritance.

  Was this answer useful?  Yes

As per my understanding, the main difference is where to use Runnable and where to use Thread class.
 Thread class is having some specialized method so if you want to create the specialized thread which will use those methods then create thread using Thread class.

But if you want only some work has to be done by thread means if Thread only meant to execute its run method then better to use Runnable interface to create the thread.

  Was this answer useful?  Yes

swapan

  • Jan 4th, 2012
 

Java cannot support the multi-inheritence. If use the Thread class to create ant thread at the same time you cant extends the any other class. Suppose if you developed any application using AWT or SWING you must be extends JFrame class at the same time you cant extends thread class. So at that time we can go to the Runnable interface.

  Was this answer useful?  Yes

sampra

  • Mar 6th, 2012
 

if we are extending thread class then we dont have chance to extend other classs where as if we implemnting runnable interface then we can do other class implement

  Was this answer useful?  Yes

KMS

  • Jun 28th, 2012
 

Runnable is an interface and Thread is an class
In java we can implement multiple interfaces but we can only extend one class. It can use depends upon your task.

If you use Thread class, unique object will be used the single thread resource.
If you use Interfaces many object will use the same thread resource. Any how It will be synchronized one.

  Was this answer useful?  Yes

thread class can be extend only one class where as we using runnable interface can be implement many classes.so runnable interface is the best to implement the block of code

  Was this answer useful?  Yes

AMIT GURJAR

  • Sep 21st, 2012
 

Sir please tell me that when a thread never return desired result then why we use it?

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions