With Runnable Interface
public class InterfaceThread implements Runnable{ /* Making the class InterfaceThread a thread
by implementing Runnable interface */
public static void main(String[] args) {
Thread t=new Thread(new InterfaceThread());
t.start(); //When start method is called run() method is executed
}
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(InterfaceThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Output:
Thread is started
0
1
2
3
4