Second method to achieve Multi-threading
Implementing a runnable Interface
Java runnable is an interface used to execute code on a concurrent thread.
It is an interface which is implemented by any class if we want that the
instances of that class should be executed by a thread.
The runnable interface has an undefined method run() with void as return
type, and it takes in no arguments. The method summary of the run()
method is given below-
Method Description
public This method takes in no arguments. When the object of a class
void implementing Runnable class is used to create a thread, then the
run() run method is invoked in the thread which executes separately.
The runnable interface provides a standard set of
rules for the instances of classes which wish to
execute code when they are active. The most
common use case of the Runnable interface is
when we want only to override the run method.
When a thread is started by the object of any
class which is implementing Runnable, then it
invokes the run method in the separately
executing thread.
A class that implements Runnable runs on a
different thread without subclassing Thread as it instantiates a Thread
instance and passes itself in as the target. This becomes important as classes
should not be subclassed unless there is an intention of modifying or
enhancing the fundamental behavior of the class.
Runnable class is extensively used in network programming as each thread
represents a separate flow of control. Also in multi-threaded programming,
Runnable class is used. This interface is present in [Link] package.
Implementing Runnable
It is the easiest way to create a thread by implementing Runnable. One can
create a thread on any object by implementing Runnable. To implement a
Runnable, one has only to implement the run method.
public void run()
In this method, we have the code which we want to execute on a concurrent
thread. In this method, we can use variables, instantiate classes, and perform
an action like the same way the main thread does. The thread remains until
the return of this method. The run method establishes an entry point to a new
thread.
Thread vs. Runnable
There are several differences between Thread class and Runnable interface
based on their performance, memory usage, and composition.
By extending thread, there is overhead of additional methods, i.e. they
consume excess or indirect memory, computation time, or other
resources.
Since in Java, we can only extend one class, and therefore if we extend
Thread class, then we will not be able to extend any other class. That is
why we should implement Runnable interface to create a thread.
Runnable makes the code more flexible, if we are extending a thread,
then our code will only be in a thread whereas, in case of runnable, one
can pass it in various executor services, or pass it to the single-
threaded environment.
Maintenance of the code is easy if we implement the Runnable
interface.
We can achieve basic functionality of a thread by extending Thread
class because it provides some inbuilt methods like yield(), interrupt()
etc. that are not available in Runnable interface.
We can clearly see from the above points that implementing
runnable is better approach.
Multi-threading using single run()
Previously we saw how to achieve multi-threading with multiple run() or by
overriding run(). Now let’s see how to achieve it by making use of single run().
Let us try to understand using the same example,