1 // Test that Thread.sleep() is accurate 2 // and that nanoTime actually measures in nanoseconds. 3 4 public class Thread_Sleep_2 5 { main(String args[])6 public static void main(String args[]) 7 { 8 try 9 { 10 boolean ok = true; 11 for (int i = 0; i < 100; i++) 12 { 13 long start = System.nanoTime(); 14 Thread.sleep(10); 15 long end = System.nanoTime(); 16 if ((end - start) < 10000000) 17 { 18 System.out.print ("failed, iteration "); 19 System.out.print (i); 20 System.out.print (", time "); 21 System.out.print (end - start); 22 System.out.println ("ns"); 23 ok = false; 24 } 25 } 26 if (ok) 27 System.out.println ("ok"); 28 } 29 catch (InterruptedException x) 30 { 31 System.out.println("error: Thread interrupted."); 32 } 33 } 34 } 35