Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. // creating buffer queue
  6. Queue q = new Queue();
  7.  
  8. for(int i=0; i < 5; i++)
  9. new Consumer(q , i);
  10.  
  11. for(int i=0; i < 4; i++)
  12. new Producer(q , i);
  13. }
  14. }
  15.  
  16. public class Producer implements Runnable
  17. {
  18. Queue q;
  19. private int id ;
  20. Producer(Queue q, int id) {
  21. this.q = q;
  22. this.id=id;
  23. new Thread(this, "Producer").start();
  24. }
  25.  
  26. public void run() {
  27. for(int i=0; i < 50; i++)
  28. // producer put items
  29. q.put(id ,i);
  30. }
  31. }
  32.  
  33. public class Consumer implements Runnable
  34. {
  35. Queue q;
  36. private int id;
  37. Consumer(Queue q, int id ){
  38. this.q = q;
  39. this.id=id ;
  40. new Thread(this, "Consumer").start();
  41. }
  42.  
  43. public void run()
  44. {
  45. for(int i=0; i < 50; i++)
  46. // consumer get items
  47. q.get(id) ;
  48. }
  49. }
  50.  
  51.  
  52. import java.util.Random;
  53. import java.util.concurrent.Semaphore;
  54.  
  55.  
  56.  
  57.  
  58. public class Queue
  59. {
  60. static Random r= new Random();
  61. // an item
  62. int item;
  63. String data ;
  64.  
  65. // semCon initialized with 0 permits
  66. // to ensure put() executes first
  67. static Semaphore semCon = new Semaphore(0);
  68.  
  69. static Semaphore semProd = new Semaphore(1);
  70.  
  71. // to get an item from buffer
  72. void get(int id)
  73. {
  74.  
  75. String data2 ;
  76. try {
  77. losoweOpóźnienie2();
  78. // Before consumer can consume an item,
  79. // it must acquire a permit from semCon
  80. semCon.acquire();
  81. }
  82. catch(InterruptedException e) {
  83. System.out.println("InterruptedException caught");
  84. }
  85.  
  86. // consumer consuming an item
  87. data2="[C-" + id + "," + item + "]" ;
  88. System.out.println(data2 + " >> Dana= " + data);
  89.  
  90. // After consumer consumes the item,
  91. // it releases semProd to notify producer
  92. semProd.release();
  93. }
  94.  
  95. // to put an item in buffer
  96. void put(int id, int item)
  97. {
  98.  
  99. try {
  100. losoweOpóźnienie1();
  101. // Before producer can produce an item,
  102. // it must acquire a permit from semProd
  103. semProd.acquire();
  104. } catch(InterruptedException e) {
  105. System.out.println("InterruptedException caught");
  106. }
  107.  
  108. // producer producing an item
  109. this.item = item;
  110. data= "[P-" + id + "," + item + "," + r.nextInt(100) + "]";
  111.  
  112. // After producer produces the item,
  113. // it releases semCon to notify consumer
  114. semCon.release();
  115. }
  116. private static void losoweOpóźnienie1() throws InterruptedException {
  117. Thread.sleep(r.nextInt(10-1));
  118. }
  119. private static void losoweOpóźnienie2() throws InterruptedException {
  120. Thread.sleep(r.nextInt(12-2));
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement