/**
 * Object monitor
 *
 * Demonstrates object monitor thread synchronization.
 */

class Counter
{
    /**
     * Increment
     *
     * params:  
     * return:  
     */
    public method increment ()
    {
        System.Concurrent.Monitor.acquire(self)

        @count.increment()

        System.Concurrent.Monitor.release(self)
    }

    
    /**
     * Get count
     *
     * params:  
     * return:  count (Number)
     */
    public method get_count ()
    {
        @count
    }

    
    /**
     * Initialize
     *
     * params:  
     * return:  
     */
    method initialize ()
    {
        field @count(0)
    }
}


/**
 * Run
 *
 * params:  counter
 * return:  
 */
method run (counter)
{
    counter.increment()
}


/**
 * Main
 *
 * params:  
 * return:  
 */
method main ()
{
    counter = Counter.new()
    run = get_method("run")
    threads = Array.new()

    // create threads
    for (i; 0..4) {
        threads.add(System.Concurrent.Thread.new(run))
    }

    // start threads
    for (i; 0..4) {
        threads[i].start(self, counter)
    }

    // join threads
    for (i; 0..4) {
        threads[i].join()
    }

    print("count: " + counter.get_count())
}

main()