Skip to content

Instantly share code, notes, and snippets.

@yloiseau
Created July 24, 2015 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yloiseau/a46f59c2722ff258623b to your computer and use it in GitHub Desktop.
Save yloiseau/a46f59c2722ff258623b to your computer and use it in GitHub Desktop.
golo concurrency

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
@artpej
Copy link

artpej commented Jul 24, 2015

I got it:

sleep( longValue(RND.nextInt(1000)) );

It seems that this exeption terminate the runnable silently:

java.lang.NoSuchMethodError: sleep(Ljava/lang/Object;)Ljava/lang/Object;

@yloiseau
Copy link
Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment