KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > standard > sleep


1 package kawa.standard;
2 import kawa.lang.*;
3 import gnu.math.*;
4
5 /** Implements the extended procedure "sleep". */
6
7 public class sleep
8 {
9   public static void sleep (Quantity q)
10    {
11      Unit u = q.unit();
12      double seconds;
13      // If q is either dimensionless or its unit is a multiple of Unit.second:
14
if (u == Unit.Empty
15      || u.dimensions() == Unit.second.dimensions())
16        seconds = q.doubleValue();
17      else
18        throw new GenericError("bad unit for sleep");
19      long millis = (long) (seconds * 1000.0);
20      int nanos = (int) (seconds * 1e9 - millis * 1e6);
21      try
22        {
23      Thread.sleep (millis, nanos);
24        }
25      catch (InterruptedException JavaDoc ex)
26        {
27      throw new GenericError("sleep was interrupted");
28        }
29    }
30   
31 }
32
Popular Tags