/** AWK~plus - Atomic.
 */
# 1から100までの合計値を求める.(Find the total value from 1 to 100)
# インタプリタでは、(a = ++b)で暗黙の排他制御を行っています.
# (In the interpreter, implicit exclusive control is performed with (a = ++ b))
# このままでは、Groovyでは、無理なようです.(As it is, Groovy seems impossible)
    val number = 100
    val multi = 8 # スレッドの多重度(Thread multiplicity)
    val guard = Barrier(multi) # バリア(Barrier)
    var count = 0
    var total = 0
BEGIN {
    val T = new AtomicMap()
    function party(id: Integer) {
        var n = 0
        printf "(Join %d)", id
        Join(guard) # パーティの参加者が揃うのを待ち合わせる(Wait for party participants)
        while (number >= (n = ++count)) { # ここで競合させる(Let it compete here)
            total += n
            printf "(%d %d %d)", id, n, total
        }
        printf "(end %d)", id
    }
    printf ".'count' %s 'total' %s\n", count.getClass(), total.getClass()
    for (var i = 1; multi >= i; ++i) # パーティに参加(Join a party)
        T[i] = Future(.party(i))
    Join(T) # パーティ終了を待ち合わせる(Wait for the end of the party)
    printf "\n.'count' %s 'total' %s\n", count.getClass(), total.getClass()
    printf ". atomic (%d) Expect: 5050 Actual: %d\n", multi, total
    assertEquals(5050, total, "!")
    print "."
}