1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Java Thread class. */ public class PauseThreadUsingSleep { public static void main(String[] args) { /* * To pause execution of a thread, use * void sleep(int milliseconds) method of Thread class. * * This is a static method and causes the suspension of the thread * for specified period of time. * * Please note that, this method may throw InterruptedException. */ System.out.println("Print number after pausing for 1000 milliseconds"); try{ for(int i=0; i< 5; i++){ System.out.println(i); /* * This thread will pause for 1000 milliseconds after * printing each number. */ Thread.sleep(1000); } } catch(InterruptedException ie){ System.out.println("Thread interrupted !" + ie); } } } /* Output of this example would be Print number after pausing for 1000 milliseconds 0 1 2 3 4 */ |
Is there any need to extend Thread class?Without it how does it run??