alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (threadring.groovy)

This example Groovy source code file (threadring.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

integer, integer, messagethread, messagethread, thread, thread, thread_count, thread_count

The Groovy threadring.groovy source code

/**
 * The Computer Language Benchmarks Game
 * http://shootout.alioth.debian.org/
 * contributed by Klaus Friedel
 * converted to Groovy by Danno Ferrin
 */

import java.util.concurrent.locks.LockSupport;

public static class MessageThread extends Thread {
  MessageThread nextThread;
  volatile Integer message;

  public MessageThread(MessageThread nextThread, int name) {
    super(""+name);
    this.nextThread = nextThread;
  }

  public void run() {
    while(true) nextThread.enqueue(dequeue());
  }

  public void enqueue(Integer hopsRemaining) {
    if(hopsRemaining == 0){
      System.out.println(getName());
      System.exit(0);
    }
    // as only one message populates the ring, it's impossible
    // that queue is not empty
    message = hopsRemaining - 1;
    LockSupport.unpark(this); // work waiting...
  }

  private Integer dequeue(){
    while(message == null){
      LockSupport.park();
    }
    Integer msg = message;
    message = null;
    return msg;
  }
}

int THREAD_COUNT = 503;
int hopCount = Integer.parseInt(args[0]);

MessageThread first = null;
MessageThread last = null;
for (int i = THREAD_COUNT; i >= 1 ; i--) {
  first = new MessageThread(first, i);
  if(i == THREAD_COUNT) last = first;
}
// close the ring:
last.nextThread = first;

// start all Threads
MessageThread t = first;
t.start();
t = t.nextThread;
while(t != first) {
  t.start();
  t = t.nextThread;
}
// inject message
first.enqueue(hopCount);
first.join(); // wait for System.exit

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy threadring.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.