What am I missing?
java:
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutorService;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static gololang.Predefined.*;
class TestA {
private static final Random RND = new Random();
public static void err(String msg) {
System.err.println(msg);
}
public static void routine(ExecutorService es, Runnable clos) {
es.submit(clos);
}
public static void stopExec(ExecutorService es, int timeout) {
try {
es.shutdown();
es.awaitTermination(timeout, TimeUnit.SECONDS);
} catch (InterruptedException e) {
err("termination interrupted");
} finally {
if (!es.isTerminated()) {
err("killing non-finished tasks");
}
es.shutdownNow();
}
}
public static void f(int n) {
println("== " + n);
try {
sleep(RND.nextInt(1000));
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
println(">> " + n);
}
public static void main(final String[] args) {
ExecutorService es = newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int n = i;
routine(es, () -> f(n));
}
stopExec(es, 60);
err("# finished");
}
}
$ javac TestA.java && java TestA
== 1
== 9
== 7
== 8
== 6
== 5
== 3
== 4
== 2
== 0
>> 9
>> 8
>> 1
>> 2
>> 6
>> 0
>> 3
>> 4
>> 5
>> 7
# finished
Golo:
module TestA
import java.util.concurrent.Executors
import java.util.concurrent
import java.util.Random
let RND = Random()
function err = |msg| {
java.lang.System.err(): println(msg)
}
function routine = |es, clos| {
es: submit(clos)
}
function stopExec = |es, timeout| {
try {
es: shutdown()
es: awaitTermination(timeout, TimeUnit.SECONDS())
} catch (e) {
case {
when e oftype InterruptedException.class {
err("termination interrupted")
}
otherwise { err(e) }
}
} finally {
if not es: isTerminated() {
err("killing non-finished tasks")
}
es: shutdownNow()
}
}
function f = |n| {
println("== "+ n)
sleep(RND: nextInt(1000))
println(">> " + n)
}
function main = |args| {
let es = newCachedThreadPool()
for (var i = 0, i < 10, i = i + 1) {
let n = i
routine(es, -> f(n))
}
stopExec(es, 60)
err("# finished")
}
$ golo golo --files TestA.golo
== 0
== 1
== 2
== 3
== 4
== 5
== 6
== 7
== 9
== 8
# finished
I got it:
It seems that this exeption terminate the runnable silently: