Sleep: Difference between revisions

3,005 bytes added ,  3 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
(Add Lang example)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 5 users not shown)
Line 898:
 
=={{header|COBOL}}==
COBOL 2023 introduced the <code>AFTER</code> phrase of the <code>CONTINUE</code> statement to specify a time period in seconds for which execution will be suspended, which, depending on implementation, could be not an integer.
There are two methods for putting the program to sleep, both requiring unofficial extensions.
{{works with|COBOL 2023}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Sleep-In-Seconds.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Seconds-To-Sleep USAGE IS FLOAT-LONG.
 
PROCEDURE DIVISION.
ACCEPT Seconds-To-Sleep
DISPLAY "Sleeping..."
CONTINUE AFTER Seconds-To-Sleep SECONDS
DISPLAY "Awake!"
GOBACK.
 
END PROGRAM Sleep-In-Seconds.</syntaxhighlight>
 
Prior to this there were two methods for putting the program to sleep using unofficial extensions.
 
The first expects the amount of time to be in seconds.
{{works with|ACUCOBOL-GT}}
{{works with|OpenCOBOLGnuCOBOL}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Sleep-In-Seconds.
Line 907 ⟶ 926:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Seconds-To-Sleep USAGE IS COMP-2.
*> Note: COMP-2, while supported on most implementations, is
*> non-standard. FLOAT-SHORT is the proper USAGE for Native
*> IEEE 754 Binary64 Floating-point data items.
 
PROCEDURE DIVISION.
ACCEPT Seconds-To-Sleep
 
DISPLAY "Sleeping..."
 
CALL "C$SLEEP" USING BY CONTENT Seconds-To-Sleep
 
DISPLAY "Awake!"
GOBACK.
 
END PROGRAM Sleep-In-Seconds.</syntaxhighlight>
GOBACK
.</syntaxhighlight>
 
While the second expects the time to be in nanoseconds. Note: Windows systems can only sleep to the nearest millisecond.
{{works with|OpenCOBOLGnuCOBOL}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Sleep-In-Nanoseconds.
OPTIONS.
DEFAULT ROUNDED MODE IS NEAREST-AWAY-FROM-ZERO.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Seconds-To-Sleep USAGE COMPIS FLOAT-2LONG.
01 Nanoseconds-To-Sleep USAGE COMPIS FLOAT-2LONG.
01 Nanoseconds-Per-Second CONSTANT AS 1000000000.
 
PROCEDURE DIVISION.
ACCEPT Seconds-To-Sleep
MULTIPLYCOMPUTE SecondsNanoseconds-To-Sleep BY Nanoseconds-Per-Second
GIVING= NanosecondsSeconds-To-Sleep * Nanoseconds-Per-Second
END-COMPUTE
 
DISPLAY "Sleeping..."
 
CALL "CBL_OC_NANOSLEEP"
USING BY CONTENT Nanoseconds-To-Sleep
END-CALL
 
DISPLAY "Awake!"
GOBACK.
 
END PROGRAM Sleep-In-Nanoseconds.</syntaxhighlight>
GOBACK
.</syntaxhighlight>
 
=={{header|Common Lisp}}==
Line 1,081 ⟶ 1,103:
})
}</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
print "How many seconds should I sleep? "
sec = number input
print "Sleeping ..."
sleep sec
print "Awake!"
</syntaxhighlight>
 
=={{header|EGL}}==
Line 1,155 ⟶ 1,186:
 
The time can be a decimal like 1.5 though the actual resolution of <code>sleep-for</code> depends on the operating system. The similar <code>sit-for</code> stops sleeping if there's pending keyboard input.
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|The pause command takes milliseconds, we adjust to seconds|^
fun main = int by List args
int seconds
if args.length == 1 do seconds = int!args[0] end
if seconds == 0
seconds = ask(int, "Enter number of seconds to sleep: ")
end
writeLine("Sleeping...")
pause(1000 * seconds)
writeLine("Awake!")
return 0
end
exit main(Runtime.args)
</syntaxhighlight>
{{out}}
Sample session:
<pre>
Enter number of seconds to sleep: 7
Sleeping...
Awake!
</pre>
 
=={{header|Erlang}}==
Line 1,445 ⟶ 1,500:
}
}</syntaxhighlight>
 
===Using Java 8===
<syntaxhighlight lang="java">
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
 
public final class Sleep {
public static void main(String[] args) {
try {
System.out.println("Enter time to sleep in milliseconds:");
Scanner scanner = new Scanner(System.in);
final int delay = scanner.nextInt();
scanner.close();
System.out.println("Sleeping...");
TimeUnit.MILLISECONDS.sleep(delay);
System.out.println("Awake!");
} catch (InputMismatchException | InterruptedException exception) {
exception.printStackTrace(System.err);;
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
Enter time to sleep in milliseconds:
4321
Sleeping...
Awake!
 
</pre>
 
=={{header|JavaScript}}==
Line 2,228 ⟶ 2,317:
<pre>
1 2 3 4 5 6 7 8 9 10
</pre>
 
=={{header|RPL}}==
{| class="wikitable"
! RPL code
! Comment
|-
|
CLLD "Sleeping..." 1 DISP
WAIT
CLMF "Awake!"
≫ ‘'''SLEEP'''’ STO
|
'''SLEEP''' ''( seconds -- "Awake!" )''
clear screen and display message on top of screen
sleep the given number of seconds
reactivate the stack display
|}
{{in}}
<pre>
10 SLEEP
</pre>
 
Line 2,552 ⟶ 2,664:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "timer" for Timer
import "io" for Stdin, Stdout
 
9,476

edits