Advertisement
Guest User

Untitled

a guest
May 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package pr4;
  7. import java.util.Scanner;
  8. /**
  9. *
  10. * @author student
  11. */
  12. class Bufor
  13. {
  14. final int rozmiar = 100;
  15. long bufor[] = new long[rozmiar];
  16. int mp, mk, zapas;
  17.  
  18.  
  19. }
  20.  
  21. class ProdKons extends Thread
  22. {
  23. int nr;
  24. public ProdKons(int nr)
  25. {
  26. this.nr=nr;
  27. if(nr<10)
  28. this.setName("Producent:"+nr);
  29. else
  30. this.setName("Konsument:"+nr);
  31. }
  32.  
  33. static Bufor buf=new Bufor();
  34. static long t0;
  35. static void rozgrzewka(int pmp, int pmk, int pzapas)
  36. {
  37. buf.mp=pmp;
  38. buf.mk=pmk;
  39. buf.zapas=pzapas;
  40. t0=System.currentTimeMillis();
  41. }
  42. synchronized void producent()
  43. {
  44. if(buf.zapas<buf.rozmiar){
  45. {long czas=(long)Math.round(Math.random()*2);
  46.  
  47. try
  48. {this.sleep(3);}
  49. catch (Exception e) {System.out.print("wyjątek -"+e);}
  50.  
  51. }
  52.  
  53.  
  54. buf.bufor[buf.mp]= nr;
  55. buf.mp=(buf.mp+1)%buf.rozmiar;
  56. buf.zapas++;
  57. }
  58. }
  59. synchronized void konsument()
  60. {
  61. if(buf.zapas>0)
  62. {
  63. long konsumcja=buf.bufor[buf.mk];
  64. buf.mk=(buf.mk+1)%buf.rozmiar;
  65. buf.zapas--;
  66. long czas=(long)(5+Math.round(Math.random()*10));
  67. try
  68. {
  69. sleep(czas);
  70.  
  71. }
  72. catch(Exception E){System.out.print(E);
  73.  
  74. }
  75.  
  76.  
  77. }
  78.  
  79.  
  80. }
  81.  
  82.  
  83. public void run()
  84. {
  85. long tx= t0 ;
  86. while(tx-t0<100)
  87. {
  88.  
  89. if (nr<10) producent();
  90. else konsument();
  91. tx = System.currentTimeMillis();
  92. raport();
  93.  
  94. }
  95.  
  96. }
  97.  
  98. void raport()
  99. {
  100.  
  101. if(nr<20)
  102. System.out.println("Produkcja :"+this.getName());
  103. else
  104. System.out.println("Konsumpcja :"+this.getName());
  105.  
  106. }
  107.  
  108.  
  109. }
  110.  
  111. public class PR4 {
  112.  
  113. /**
  114. * @param args the command line arguments
  115. */
  116. public static void main(String[] args) {
  117.  
  118. ProdKons.rozgrzewka(0,0, 0);
  119. ProdKons P[]= new ProdKons[10];
  120. ProdKons K[]= new ProdKons[100];
  121. for (int i=0; i<10; i++)P[i]= new ProdKons(i);
  122. for (int i=0; i<10; i++)P[i].start();
  123. for (int i=0; i<100; i++)K[i]= new ProdKons(i+20);
  124. for (int i=0; i<100; i++)K[i].start();
  125. for (int i=0; i<10; i++)
  126. try{
  127. P[i].join();
  128.  
  129. }
  130. catch(InterruptedException e){System.out.println(e);}
  131.  
  132. for (int i=0; i<100; i++)
  133. try
  134. {
  135. K[i].join();
  136.  
  137. }
  138. catch(InterruptedException e) {System.out.println(e);}
  139. // TODO code application logic here
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement