Advertisement
Guest User

Untitled

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