I was thinking to improve my threading fundamentals, so I
planned to make few notes for myself that will assist me in future (Obviously
at the time of Interviews). Following questions and answers have been taken from
stackoverflow.com. This post is a sticky notes post for me.
Thread can
be instantiate with 2 different ways. You can create a class by either implementing
Runnable interface or extending Thread class.
public
class RunnableImpl implements Runnable {
public
void run() {
// TODO
Write something here.
}
}
public
class SubThread extends Thread {
public void run()
{
//TODO Write something here
}
}
When java has provided various ways of implementation, then
where these implementations should be used. Which is more commonly and
beneficial implementation among two?
1. One basic difference between these two
approaches is when class is using an interface then it has the freedom to
extend any other class. So RunnableImpl class may have multiple functionalities.
Whereas when class extends thread class, then it is bound to function as Thread
only.
2. When class is implementing Runnable interface,
then this class can act as a nonthreading instance.
At the start application used to perform in
the manner mentioned below.
public class ExecuteRun{
public
static void main(String[] args) {
Thread
runImpl = new Thread(new RunnableImpl());
runImpl.start();
}
}
But for some strange reasons, it was
decided to get rids of multithreading implementation. In this case the following
code can be changed to the following.
public
class ExecuteRun{
public
static void main(String[] args) {
new
RunnableImpl().run();
}
}
On forcing our mind to work, we will say it
is possible to do with Thread implementation as well, then why we preferred it
over other.
It was preferred because Java 1.4 caused a
memory leak when Thread was started with Run method instead Start method.
Though this issue has been resolved in Java
5 and higher versions, but people still prefer other implementation practice.
3. Other important reason to use Runnable is the
flexibility to use concurrency API introduced in Java 5. Runnable allows to
keep work loosely coupled with desired concurrency.
Caveat: Around
here, I strongly discourage the use of raw Threads. I much prefer the use of Callablesand FutureTasks (From
the javadoc: "A cancellable asynchronous computation"). The
integration of timeouts, proper cancelling and the thread pooling of the modern
concurrency support are all much more useful to me than piles of raw Threads.
Follow-up: there is a FutureTask constructor that
allows you to use Runnables (if that's what you are most comfortable with) and
still get the benefit of the modern concurrency tools. To quote the javadoc: If you don't need a particular result, consider
using constructions of the form:Future<?> f = new
FutureTask<Object>(runnable, null)
So, if we replace their runnable with
your threadA, we get the following:
new FutureTask<Object>(threadA, null)
Another option that allows you to stay closer
to Runnables is a ThreadPoolExecutor.
You can use theexecute method to pass in a Runnable to execute "the
given task sometime in the future."
If you'd like to try using a thread pool, the
code fragment above would become something like the following (using the Executors.newCachedThreadPool() factory
method):
ExecutorService es = Executors.newCachedThreadPool();
es.execute(new ThreadA());
4. One significant difference between Thread and
Runnable is the sharing of resources. In case of Runnable implementation, one resource
can be shared with multiple instances whereas it is not possible with thread
implementation. (I will try to find some appropriate example here soon)
Such a huge discussion boils down
on the following point.
Subclass must extend a class only
if it overrides methods other than Run method, otherwise it is beneficial to
implement an interface in the class.
No comments:
Post a Comment