You are on page 1of 5

public interface Runnable

The Runnable interface should be implemented by any class whose


instances are intended to be executed by a thread.
The class must define a method of no arguments called run.
This interface is designed to provide a common protocol for
objects that wish to execute code while they are active. For
example, Runnable is implemented by class Thread. Being active
simply means that a thread has been started and has not yet been
stopped.
In addition, Runnable provides the means for a class to be active
while not subclassing Thread.
o A class that implements Runnable can run without
subclassing Thread by instantiating a Thread instance and
passing itself in as the target.
In most cases, the Runnable interface should be used if you are
only planning to override the run() method and no
other Thread methods.
o This is important because classes should not be subclassed
unless the programmer intends on modifying or enhancing
the fundamental behavior of the class.

A multi threaded program contains two or more parts that can run
concurrently and each part can handle different task at the same time
making optimal use of the available resources specially when your
computer has multiple CPUs.

By definition multitasking is when multiple processes share common


processing resources such as a CPU.
Multi threading extends the idea of multitasking into applications
where you can subdivide specific operations within a single
application into individual threads. Each of the threads can run in
parallel. The OS divides processing time not only among different
applications, but also among each thread within an application.
Multi threading enables you to write in a way where multiple
activities can proceed concurrently in the same program.
An interface is a reference type in Java, it is similar to class, it is a
collection of abstract methods.
A class implements an interface, thereby inheriting the abstract methods
of the interface.
Along with abstract methods an interface may also contain constants,
default methods, static methods, and nested types. Method bodies exist
only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes
the attributes and behaviours of an object. And an interface contains
behaviours that a class implements.
Unless the class that implements the interface is abstract, all the
methods of the interface need to be defined in the class.
An interface is similar to a class in the following ways:
An interface can contain any number of methods.
An interface is written in a file with a .java extension, with the
name of the interface matching the name of the file.
The byte code of an interface appears in a .class file.
Interfaces appear in packages, and their corresponding bytecode
file must be in a directory structure that matches the package
name.
However, an interface is different from a class in several ways,
including:
You cannot instantiate an interface.
An interface does not contain any constructors.
All of the methods in an interface are abstract.
An interface cannot contain instance fields. The only fields that
can appear in an interface must be declared both static and final.
An interface is not extended by a class; it is implemented by a
class.
An interface can extend multiple interfaces.

1) Process-based Multitasking (Multiprocessing)

o Each process have its own address in memory i.e. each process
allocates separate memory area.
o Process is heavyweight.
o Cost of communication between the process is high.
o Switching from one process to another require some time for
saving and loading registers, memory maps, updating lists etc.

2) Thread-based Multitasking (Multithreading)

o Threads share the same address space.


o Thread is lightweight.
o Cost of communication between the thread is low.

Runnable Interface vs. Extending Thread Class


Extends binds two class files very closely and can cause some pretty
hard to deal with code.
Both approaches do the same job but there have been some differences.
The most common difference is
1. When you extend Thread class, after that you cant extend any other
class which you required. (As you know, Java does not allow
inheriting more than one class).
2. When you implements Runnable, you can save a space for your class
to extend any other class in future or now.
However, one significant difference between implementing Runnable
and extending Thread is that by extending Thread, each of your threads
has a unique object associated with it, whereas implementing Runnable,
many threads can share the same object instance.

And some more points:


1) Implementing Runnable is the preferred way to do it. Here, youre not
really specializing or modifying the threads behavior. Youre just
giving the thread something to run. That means composition is the better
way to go.

2) Java only supports single inheritance, so you can only extend one
class.

3) Instantiating an interface gives a cleaner separation between your


code and the implementation of threads.

4) Implementing Runnable makes your class more flexible. If you


extend thread then the action youre doing is always going to be in a
thread. However, if you extend Runnable it doesnt have to be. You can
run it in a thread, or pass it to some kind of executor service, or just pass
it around as a task within a single threaded application.

5) By extending Thread, each of your threads has a unique object


associated with it, whereas implementing Runnable, many threads can
share the same runnable instance.

You might also like