Example: Sleep

Description

Write a program that does the following in this order:

  1. Input an amount of time to sleep in whatever units are most natural for your language (milliseconds, seconds, ticks, etc.). This unit should be noted in comments or in a description.
  2. Print "Sleeping..."
  3. Sleep the main thread for the given amount of time.
  4. Print "Awake!"
  5. End.

Notes

The feature sleep is defined in the library class EXECUTION_ENVIRONMENT. So the demonstration class APPLICATION inherits from EXECUTION_ENVIRONMENT in order to make sleep available.

sleep takes an argument which declares the number of nanoseconds to suspend the thread's execution.

Source

Problem description from Rosetta Code

Solution

class APPLICATION inherit EXECUTION_ENVIRONMENT create make feature -- Initialization make -- Sleep for a given number of nanoseconds. do print ("Enter a number of nanoseconds: ") io.read_integer_64 print ("Sleeping...%N") sleep (io.last_integer_64) print ("Awake!%N") end end

Output (sleeping 10 seconds)

Enter a number of nanoseconds: 10000000000 Sleeping... Awake!

cached: 03/19/2024 3:49:01.000 AM