[Shootout-list] process creation & message passing

Isaac Gouy igouy2@yahoo.com
Tue, 19 Oct 2004 10:22:21 -0700 (PDT)


chain
- create a chain of 1000 processes/threads/... 
- each process increments a count
- repeat n times
: print the final process/thread/count 
(1 parameter, n: how many create cycles)
scale by increasing number of create cycles.


public class chain {
   static final int LENGTH = 1000;

   public static void main(String args[]) {
      int n = 1;
      if (args.length > 0) n = Integer.parseInt(args[0]);

      int count = 0;
      while (n-- > 0) {
         Link chain = null;
         Link link = null;

         synchronized(Link.hold){
            for (int i=1; i<=LENGTH; i++){
               link = new Link(chain,count);             
               link.start();
               chain = link;
               count = chain.count;
            }
         }
      }
      System.out.println(count);
   }
}

class Link extends Thread {
   public static Boolean hold = new Boolean(true);
   public int count;
   private Link next;
   
   Link(Link t, int i){
      next = t;
      count = i + 1;
   }

   public void run() {
      synchronized(hold){}    
   }
}

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com