Java Thread Example by extending Thread class


\begin{lstlisting}
class Multi extends Thread{
public void run(){
System.out.p...
...ain(String args[]){
Multi t1=new Multi();
t1.start();
}
}
\end{lstlisting}

Above code creates thread is running... as output. Following code illustrates Java Thread Example by implementing Runnable interface.


\begin{lstlisting}
class Multi3 implements Runnable{
public void run(){
System...
...=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
\end{lstlisting}

Above code produces thread is running... as output. If you are not extending the Thread class,your class object would not be treated as a thread object.So you need to explicitely create Thread class object.We are passing the object of your class that implements Runnable so that your class run() method may execute.