Multi-Threading in JAVA
[[Threads]] can be created by using two mechanisms in java
Extending the Thread class
Implementing the Runnable Interface
the import package for thread class in java is java.lang.Thread
This class overrides the run() method available in the thread class. A thread begins its life inside run() method.
We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the [[Threads]] object
Extending the Thread class
Sample code
`
Implementing the Runnable Interface
Sample code
Thread class vs Runnable Interface
-> If we extend the Thread class, our class cannot extend any other class because Java doesnt support multiple inheritance which is opposite for implementing Runnable interface to our custom classes.
-> Using runnable will give you an object that can be shared amongst multiple threads
Last updated