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
What is the full meaning of java?
How many bytes is a url?
Difference between Linked list and Queue?
How to provide security in java
Can a constructor be made final?
What is the concatenation operator in java?
What are the files generated after using IDL to java compiler?
What is the purpose of the return statement?
How would overload a function based on return type?
Which command from the jdk compiles a java program?
Why is java called the platform independent programming language?
How to reverse string in java?
Why char array is favored over string for the storage of passwords?
What all access modifiers are allowed for top class ?
What is the main advantage of passing argument by reference?