Extending Thread Class
public class SingleThread extends Thread{ // Making the class InterfaceThread a thread by extending class Thread
@Override
public void run(){
System.out.println("Thread is started");
for(int i=0;i<5;i++){
try {
System.out.println(i);
Thread.sleep(1000); // Causes a delay of 1000 milliseconds
} catch (InterruptedException ex) {
Logger.getLogger(SingleThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
SingleThread s=new SingleThread();
s.start(); //When start method is called run() method is executed
}
}
Output:
Thread is started
0
1
2
3
4