PageRenderTime 40ms CodeModel.GetById 11ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

/bench/kilim/bench/ThreadBench.java

http://github.com/kilim/kilim
Java | 141 lines | 119 code | 15 blank | 7 comment | 21 complexity | c33c453f30d3e992e24f52fcd200918a MD5 | raw file
  1/* Copyright (c) 2006, Sriram Srinivasan
  2 *
  3 * You may distribute this software under the terms of the license 
  4 * specified in the file "License"
  5 */
  6
  7package kilim.bench;
  8
  9public class ThreadBench extends Thread {
 10    ThreadBench next;
 11    int val = -1; // This value is filled by the previous process before this process is
 12    
 13    public static boolean tracing = false;
 14    static long startTime;
 15    
 16    public static void main(String[] args) {
 17        int n = 500;
 18        int k = 10000;
 19        try {
 20            for (int i = 0; i < args.length; i++) {
 21                String arg = args[i];
 22                if (arg.equals("-k")) {
 23                    k = Integer.parseInt(args[++i]);
 24                } else if (arg.equals("-n")) {
 25                    n = Integer.parseInt(args[++i]);
 26                } else if (arg.equals("-t")) {
 27                    tracing = true;
 28                }
 29            }
 30        } 
 31        catch (NumberFormatException e) {
 32            System.err.println("Integer argument expected");
 33        }
 34        startTime = System.currentTimeMillis();
 35        bench(k, n);
 36    }
 37    
 38    static void bench(int k, int n) {
 39        Sink sink = new Sink();
 40        ThreadBench next = sink;
 41        for (int i = n; i >= 1; i--) {
 42            next.start();
 43            next = new Copy(i, next);
 44        }
 45        next.start();
 46        Source source = new Source(k, next);
 47        sink.source = source;
 48        source.start();
 49        try {
 50            source.join();
 51        } catch (InterruptedException ie) {}
 52        System.out.println("Done");
 53    }
 54    
 55    ThreadBench() {
 56    }
 57    
 58    synchronized void write(int v) {
 59        while (val != -1) {
 60            try {
 61                wait();
 62            } catch (InterruptedException ie) {ie.printStackTrace();}
 63        }
 64        val = v;
 65        notify();
 66    }
 67    
 68    synchronized int read() {
 69        while (val == -1) {
 70            try {
 71                wait();
 72            } catch (InterruptedException ie) {ie.printStackTrace();}       
 73        }
 74        int v = val;
 75        val = -1;
 76        notify();
 77        return v;
 78    }
 79    
 80    static class Copy extends ThreadBench {
 81        int  id;
 82        // woken up
 83        Copy(int aid, ThreadBench p) {
 84            id = aid;
 85            next = p;
 86        }
 87        
 88        public void run() {
 89            while (true) {
 90                int v = read();
 91                if (tracing) 
 92                    System.out.println(this.toString() + " copying number " + v);
 93                next.write(v);
 94                if (v == 0) break; 
 95            }
 96        }
 97        public String toString() {return "copy " + id;}
 98    }
 99
100    static class Sink extends ThreadBench {
101        ThreadBench source;
102        public  void run() {
103            int v;
104            int i = 0;
105            while (true) {
106                v = read();
107                i++;
108                if(tracing) 
109                    System.out.printf("sink: receiving number %d\n-----\n", v);
110                if (v == 0) {
111                    System.out.println("Elapsed time: " + 
112                            (System.currentTimeMillis() - startTime)
113                            + " ms, iterations = " + i);
114                    System.exit(0);
115                }
116            }
117        }
118        public String toString() {return "sink";}
119    }
120
121    static class Source extends ThreadBench {
122        int loops;
123
124        Source(int k, ThreadBench p) {
125            loops = k;
126            next = p;
127        }
128        
129        public void run() {
130            for (int i = 1; i <= loops; i++) {
131                if(tracing) 
132                    System.out.printf("source: sending number %d\n", i);
133                next.write(i);
134            }
135            // Kill
136            next.write(0);
137        }
138        public String toString() {return "source";}
139    }
140}
141