Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
16
Task/Concurrent-computing/Dart/concurrent-computing-1.dart
Normal file
16
Task/Concurrent-computing/Dart/concurrent-computing-1.dart
Normal 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");
|
||||
59
Task/Concurrent-computing/Dart/concurrent-computing-2.dart
Normal file
59
Task/Concurrent-computing/Dart/concurrent-computing-2.dart
Normal 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");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue