The Computer Language
Benchmarks Game

thread-ring Java #7 program

source code

/**
 * The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 * contributed by Klaus Friedel
 * modified by Daryl Griffith
 */

import java.util.concurrent.locks.LockSupport;

public class threadring extends Thread {

    static final int THREAD_COUNT = 503;

    threadring nextThread;
    volatile boolean waiting = true;
    int message;

    public threadring(int name) {
        super(Integer.toString(name));
    }

    @Override
    public void run() {
        for (;;) {
            while (waiting) {
                LockSupport.park();
            }
            if (message == 0) {
                System.out.println(getName());
                System.exit(0);
            }
            waiting = true;
            nextThread.message = message - 1;
            nextThread.waiting = false;
            LockSupport.unpark(nextThread);
        }
    }

    public static void main(String args[]) throws Exception {
        threadring first = new threadring(1);
        threadring current = new threadring(2);

        first.start(); // Thread 1
        first.nextThread = current;
        first.message = Integer.parseInt(args[0]);
        first.waiting = false;
        for (int i = 3; i < THREAD_COUNT; i++) {
            current.nextThread = new threadring(i);
            current.start();
            current = current.nextThread;
        }
        current.nextThread = new threadring(THREAD_COUNT);
        current.start(); // Thread 502
        current = current.nextThread;
        current.nextThread = first;
        current.start(); // Thread 503
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
java 9.0.1
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)


Wed, 25 Oct 2017 22:31:07 GMT

MAKE:
mv threadring.java-7.java threadring.java
/opt/src/jdk-9.0.1/bin/javac -d .  threadring.java

1.05s to complete and log all make actions

COMMAND LINE:
/opt/src/jdk-9.0.1/bin/java   threadring 50000000

PROGRAM OUTPUT:
292