cancel.j
use io;
use task;
use time;
def worker as task of int init spawn {
def rounds as int init 0;
try {
while (true) {
time.sleep(time.fromMilliseconds(100));
$rounds = $rounds + 1;
}
} catch (e) {
io.printf("worker stopped: %s after %d rounds\n", $e.kind, $rounds);
}
return $rounds;
};
def slow as task of int init spawn {
time.sleep(time.fromSeconds(5));
return 1;
};
time.sleep(time.fromMilliseconds(350));
task.cancel($worker);
task.wait($worker);
try {
task.waitTimeout($slow, 200);
} catch (e) {
io.printf("gave up: %s\n", $e.kind);
task.cancel($slow);
task.discard($slow);
}