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