Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

We have two methods to create methods the threads.
1. Implementing runnable interface
2. Extending to thread class and overriding run method.
Among these two which one is better and why?
Please explain me in detail.

Answer Posted / mira

The Runnable technique is particularly convenient when you
want to create just a single thread to carry out a specific
task. The run() method will have access to the instance
variables in the Runnable object. For example, a common
approach to applet animation is to make the applet
Runnable. The run() method then has access to the applet's
variables, which could be parameters passed in the applet
tags or set by the user via the graphical interface.

Note also that since there are no restrictions on the
number of interfaces that a class can implement, it is easy
to add Runnable to a class.

On the other hand, if you want to create multiple threads,
then it usually makes more sense to use a Thread subclass.
This helps to better conceptualize the threads as
independent objects, each with its own independent sets of
variables (as opposed to sharing variables in the Runnable
object.) You can set the values of whatever parameters they
need via their constructors or "setter" methods.

Is This Answer Correct ?    0 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is linked hashset and its features?

995


Is null an object java?

931


What if I write static public void instead of public static void in java?

1032


What is finalize()? Is finalize() similar to a destructor?

911


what is meant by abstract class?

1063


What are the different ways of creating thread?

965


What are different types of references?

934


What is variable in java?

917


Write a java program to count the number of words present in a string?

940


What is the difference between compile-time polymorphism and runtime polymorphism?

936


What is an iterator java?

1009


What is io stream in java?

974


Explain the reason behind ending a program with a system.exit(0)?

1043


How many types of exception can occur in a java program?

929


What mechanism does java use for memory management?

921