Multi Threading Threads: A thread is an object of type Thread, defined in [Link] package.
Multithreading functionality in Java is supported in four places:
Thread class Runnable interface Object class Java virtual machine Extend the Thread class itself. Implement the Runnable interface.
There are two ways to create threads:
When we have to extend a thread? The Thread class implements Runnable interface, which defines run() method. To create a thread, Thread class can be extended and the run() method must be overridden. In this method, the code to be executed by the thread is defined. Thread class defines many other methods too, which may or may not be overridden. In general, Thread should be extended to create a new thread only if methods other than run() are to be extended. Otherwise, it is probably better to implement Runnable interface. When we have to implement an interface? It is the simplest way to create a new thread. This interface defines only run() method. To implement Runnable, a class need only implement run(), which is declared as public and void in Runnable. After the class that implements Runnable is created, it can be passed in the constructor of Thread to instantiate an object of Thread.