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

Groovy example source code file (threadring.java)

This example Groovy source code file (threadring.java) 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

arraylist, cooperativethread, cooperativethread, exception, integer, list, messagethread, messagethread, scheduler, scheduler, string, thread, thread_count, thread_count, util

The Groovy threadring.java source code

/**
 * The Computer Language Benchmarks Game
 * http://shootout.alioth.debian.org/
 * contributed by Klaus Friedel
 */

import java.util.*;

public class threadring {

  public static long startTime;
  public static final int THREAD_COUNT = 503;

  // The scheduler for cooperative Multithreading
  static class Scheduler extends Thread{
    private final List<CooperativeThread> threads = Collections.synchronizedList(new ArrayList());
    private int rrIndex = -1;

    public void start(CooperativeThread t){
      threads.add(t);
    }

    public void run() {
      for(;;){ // Scheduler will run foerever
        CooperativeThread nextThread;
        synchronized (threads){
          rrIndex++;
          if(rrIndex >= threads.size()) rrIndex = 0;
          nextThread = threads.get(rrIndex);
        }
        nextThread.handleMessage();
      }
    }
  }

  static abstract class CooperativeThread{
    public abstract void handleMessage();
  }

  static class MessageThread extends CooperativeThread{
      MessageThread nextThread;
      String name;
      Integer msg;

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

      public void handleMessage(){
        if(msg == null) return;
        if(msg == 0){
          System.out.println(getName());
          System.exit(0);
        }
        nextThread.put(msg - 1);
        msg = null;
      }

      void put(Integer message){
        msg = message;
      }

      String getName() {
        return name;
      }
    }


  public static void main(String args[]) throws Exception{
    int hopCount = Integer.parseInt(args[0]);

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

    Scheduler scheduler = new Scheduler();
    // start all Threads
    MessageThread t = thread;
    do{
      scheduler.start(t);
      t = t.nextThread;
    }while(t != thread);
    scheduler.start();

    // inject message
    thread.put(hopCount);
  }
}

Other Groovy examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.