Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,16 @@
import 'dart:math' show Random;
main(){
enjoy() .then( (e) => print(e) );
rosetta() .then( (r) => print(r) );
code() .then( (c) => print(c) );
}
// Create random number generator
var rng = Random();
// Each function returns a future that starts after a delay
// Like using setTimeout with a Promise in Javascript
enjoy() => Future.delayed( Duration( milliseconds: rng.nextInt( 10 ) ), () => "Enjoy");
rosetta() => Future.delayed( Duration( milliseconds: rng.nextInt( 10 ) ), () => "Rosetta");
code() => Future.delayed( Duration( milliseconds: rng.nextInt( 10 ) ), () => "Code");

View file

@ -0,0 +1,59 @@
import 'dart:isolate' show Isolate, ReceivePort;
import 'dart:io' show exit, sleep;
import 'dart:math' show Random;
main() {
// Create ReceivePort to receive done messages
// Called a channel in other languages
var receiver = ReceivePort();
// Create job counter
var job_count = 3;
// Create job pool
var jobs = [ enjoy, rosetta, code ];
// Create random number generator
var rng = Random();
for ( var job in jobs ) {
// Sleep for random duration up to half a second
var sleep_time = Duration( milliseconds: rng.nextInt( 500 ) );
// Spawn Isolate to do work
// When finished the second argument will be sent to the receiver via the SendPort specified in onExit
Isolate.spawn( job, sleep_time, onExit: receiver.sendPort );
}
// Do something in main isolate
print("from main isolate\n");
// Register a listener on the ReceivePort, it gets called whenver something is sent on its SendPort
// We'll ignore the message with _ because we don't care about the data, just the event
receiver.listen( (_) {
// Decrement job counter
job_count -= 1;
// If jobs are all finished
if ( job_count == 0 ) {
print("\nall jobs finished!");
exit(0);
}
});
}
enjoy ( duration ) {
sleep( duration ) ;
print("Enjoy");
}
rosetta ( duration ) {
sleep( duration );
print("Rosetta");
}
code ( duration ) {
sleep( duration );
print("Code");
}