sleep.java 751 B

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