Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. package a;
  2. import java.util.concurrent.Semaphore;
  3.  
  4. class BoundedBuffer{
  5. private Semaphore mutex, empty, full;
  6. private int size=0, occupied, ins, outs,buffer_size;
  7. private int[] Buffer;
  8. private int max_rand = 1000;
  9.  
  10. public BoundedBuffer(int length){
  11.  
  12. this.buffer_size = length;
  13. Buffer = new int[buffer_size];
  14.  
  15. occupied = ins = outs = 0;
  16.  
  17. full = new Semaphore(0);
  18. empty = new Semaphore(10);
  19.  
  20. mutex = new Semaphore(1);
  21. }
  22.  
  23. public void printstatus() {
  24. System.out.println("Items inserted : " + ins);
  25. System.out.println("Items removed : " + outs);
  26. System.out.println("Spaces occupied : " + occupied);
  27. System.out.println("Delta : " + (ins - outs - occupied));
  28. }
  29.  
  30. public void insertItem(int Item) throws InterruptedException{
  31. while(true){
  32. empty.acquire();
  33.  
  34. mutex.acquire();
  35.  
  36. Buffer[size] = Item;
  37. size++;
  38. occupied++;
  39. ins++;
  40.  
  41. mutex.release();
  42.  
  43. full.release();
  44. }
  45. }
  46.  
  47. public int removeItem() throws InterruptedException{
  48. while(true){
  49. int Item;
  50.  
  51. full.acquire();
  52.  
  53. mutex.acquire();
  54.  
  55. size--;
  56. Item = Buffer[size];
  57. occupied--;
  58. outs++;
  59.  
  60. mutex.release();
  61.  
  62. empty.release();
  63. return Item;
  64. }
  65. }
  66. }
  67.  
  68. class Producer extends Thread {
  69.  
  70. public BoundedBuffer workspace;
  71. public int currentItem;
  72.  
  73. public Producer(BoundedBuffer workspace) {
  74. this.workspace = workspace;
  75. }
  76.  
  77. public void run() {
  78. try {
  79.  
  80. while(true) {
  81. currentItem = Math.round(Math.round(Math.random() * 100));
  82.  
  83. workspace.insertItem(currentItem);
  84.  
  85. sleep((int)(Math.random() * 30));
  86. }
  87.  
  88. } catch (InterruptedException e) {
  89. System.out.println("Producer thread exiting now");
  90. }
  91. }
  92. }
  93.  
  94. class Consumer extends Thread {
  95. public BoundedBuffer workspace;
  96. public int currentItem;
  97.  
  98. public Consumer(BoundedBuffer workspace) {
  99. this.workspace = workspace;
  100. }
  101.  
  102. public void run() {
  103. try {
  104. while(true) {
  105. currentItem = workspace.removeItem();
  106.  
  107. sleep((int)(Math.random() * 30));
  108. }
  109. } catch (InterruptedException e) {
  110. System.out.println("Consumer thread exiting now");
  111. }
  112. }
  113. }
  114.  
  115. class Watcher extends Thread {
  116. public BoundedBuffer workspace;
  117.  
  118. public Watcher(BoundedBuffer workspace) {
  119. this.workspace = workspace;
  120. }
  121.  
  122. public void run() {
  123. try {
  124. while(true) {
  125. workspace.printstatus();
  126.  
  127. sleep(2000);
  128. }
  129. } catch (InterruptedException e) {
  130. System.out.println("Watcher thread exiting now");
  131. }
  132. }
  133. }
  134.  
  135. // Main
  136. class Uildver {
  137.  
  138. public static void main(String[] args) throws InterruptedException {
  139.  
  140. int buff_size = 50;
  141. if(args.length>0) {
  142. try {
  143. buff_size = Integer.parseInt(args[0]);
  144. } catch (NumberFormatException e) {
  145. System.err.println("Argument must be an integer.");
  146. }
  147. }
  148. BoundedBuffer space = new BoundedBuffer(buff_size);
  149.  
  150. // create our threads
  151. Producer pthread = new Producer(space);
  152. Consumer cthread = new Consumer(space);
  153. Watcher wthread = new Watcher(space);
  154.  
  155. // start our threads
  156. wthread.start();
  157. pthread.start();
  158. cthread.start();
  159.  
  160. Thread.sleep(20*1000);
  161. Consumer cthread1 = new Consumer(space);
  162. cthread1.start();
  163.  
  164. Thread.sleep(20*1000);
  165. Producer pthread1 = new Producer(space);
  166. Producer pthread2 = new Producer(space);
  167. pthread1.start();
  168. pthread2.start();
  169.  
  170. Thread.sleep(20*1000);
  171. Consumer cthread2 = new Consumer(space);
  172. cthread2.start();
  173. // wait 2 minutes
  174. Thread.sleep(1 * 60 * 1000);
  175.  
  176.  
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement